Qeb Archives: LINQ

Parsing XML nrog LINQ

Ntawm no yog tus ceev kev piv txwv uas siv cov LINQ los parse ib co  XML thiab mus rau hauv lub qab zib, qab mib attributes therein.

Nov yog lub XML kuv xav parse:

<?xml version = "1.0 Tub ntxhais" encoding = "utf-8" ?>
<DeafultConfigurationSets>
  <PageLayouts>

    <PageLayout npe = "xyzzy">
      <Tabs>
        <Tab TabOrder = "1" TabLabel = "x" SharePointContentType = "ib" AdditionalConstraints = "ac1" />
        <Tab TabOrder = "2" TabLabel = "y" SharePointContentType = "b" AdditionalConstraints = "ac2" />
      </Tabs>
    </PageLayout>

    <PageLayout npe = "xyzzy2">
      <Tabs>
        <Tab TabOrder = "100" TabLabel = "x" SharePointContentType = "ib" AdditionalConstraints = "ac1" />
        <Tab TabOrder = "101" TabLabel = "y" SharePointContentType = "b" AdditionalConstraints = "ac2" />
        <Tab TabOrder = "103" TabLabel = "z" SharePointContentType = "c" AdditionalConstraints = "ac3" />
      </Tabs>
    </PageLayout>

  </PageLayouts>
</DeafultConfigurationSets>

Kuv xav tau ib co tabs tom runtime tsim los ntawm parsing cov saum no.  Kuv tabs vam li ib phab layout.  Yog kuv tus nplooj layout lub npe hu ua "xyzzy2" ces kuv xav tau tabs 100, 101 thiab 103 (tus tabs hauv <PageLayout npe = "xyzzy2">).

Ntawm no yog cov LINQ uas yog nws:

Var allTabs =
              ntawm p hauv
                  XElement.Parse(theXmlToParse).
                 
Ntsiab("PageLayouts").
                  Ntsiab("PageLayout")
              nyob qhov twg (p.Attribute("lub npe").Value.Equals("xyzzy2"))
              ntawm m hauv p.Elements("Tabs").Ntsiab("Tab")
              xaiv m;

          Results.Text = hlua.Npliag;

          foreach (Var aTab nyob allTabs)
          {
              Results.Text =
                  "Kev txiav txim tab: " + aTab.Attribute("TabOrder").Tus nqi + " |" +
                  "Tab lo cim: " + aTab.Attribute("TabLabel").Tus nqi + " | " +
                  "SharePointContentType: " + aTab.Attribute("SharePointContentType").Tus nqi + " | " +
                  "AdditionalConstraints: " + aTab.Attribute("AdditionalConstraints").Tus nqi + "r";
          }

Nyob rau saum no, kuj sib txawv rau thiab "theXmlToParse" yog ib txoj hlua kuj sib txawv thiab, tab sis, koj yuav tau siv cov load() txoj kev ntawm ib tug kwj yog hais tias koj nyiam.

"Tau" yog ib daim ntawv lo ntawm cov qhov web daim ntawv thiab thaum cov code no executes, nws zoo li no:

SNAGHTML11cd2e7c

Kuv tsis tau ua haujlwm tawm yuav txheeb soj ntsuam tsis tau li cas, li ntawd, kuv ces cia ntawd rau tus tom ntej blog ncej.

</kawg>

Subscribe rau kuv blog.

Raws li kuv nyob Twitter tom http://www.twitter.com/pagalvin