Parsing XML na LINQ

Hapa ni mfano ya haraka kwa kutumia LINQ kuchanganua baadhi  XML na kupata saa tamu, tamu sifa humo.

Hapa ni XML nataka Hazrat:

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

    <PageLayout name="xyzzy">
      <Tabo>
        <Tab TabOrder="1" TabLabel="x" SharePointContentType="a" AdditionalConstraints="ac1"/>
        <Tab TabOrder="2" TabLabel="y" SharePointContentType="b" AdditionalConstraints="ac2"/>
      </Tabo>
    </PageLayout>

    <PageLayout name="xyzzy2">
      <Tabo>
        <Tab TabOrder="100" TabLabel="x" SharePointContentType="a" AdditionalConstraints="ac1"/>
        <Tab TabOrder="101" TabLabel="y" SharePointContentType="b" AdditionalConstraints="ac2"/>
        <Tab TabOrder="103" TabLabel="z" SharePointContentType="c" AdditionalConstraints="ac3"/>
      </Tabo>
    </PageLayout>

  </PageLayouts>
</DeafultConfigurationSets>

Nataka kujenga tabo baadhi katika Runtime na parsing hapo juu.  Tabo yangu hutegemea juu ya mpangilio wa ukurasa.  Kama jina ukurasa wangu layout ni "xyzzy2" basi nataka kupata tabo 100, 101 na 103 (tabo katika <PageLayout jina = "xyzzy2">).

Hapa ni LINQ kwamba gani:

mara allTabs =
              kutoka uk katika
                  XElement.Parse(theXmlToParse).
                 
Vipengele("PageLayouts").
                  Vipengele("PageLayout")
              ambapo (p.Attribute("name").Value.Equals("xyzzy2"))
              kutoka m katika p.Elements("Tabs").Vipengele("Tab")
              kuchagua m;

          Results.Text = string.Empty;

          foreach (mara ATAB katika allTabs)
          {
              Results.Text =
                  "Tab Order: " + aTab.Attribute("TabOrder").Thamani + " |" +
                  "Tab Label: " + aTab.Attribute("TabLabel").Thamani + " | " +
                  "SharePointContentType: " + aTab.Attribute("SharePointContentType").Thamani + " | " +
                  "AdditionalConstraints: " + aTab.Attribute("AdditionalConstraints").Thamani + "\r";
          }

Katika hapo juu, kutofautiana "theXmlToParse" ni kutofautiana kamba, lakini unaweza kutumia mzigo() njia juu ya mkondo kama wewe kama.

"Matokeo" ni studio ya fomu mtandao na wakati kanuni hii executes, inaonekana kama hii:

SNAGHTML11cd2e7c

Mimi si kazi nje ya jinsi ya kutatua matokeo bado, hivyo mimi itabidi kuondoka kwamba kwa post baadaye blog.

</mwisho>

Kujiunga na blog yangu.

Kufuata yangu juu ya Twitter kwa http://www.twitter.com/pagalvin

Kuondoka Reply

Anwani yako si kuchapishwa. Mashamba required ni alama *