Tag Achiv yo: LINQ

L' XML ak LINQ

Isit la se yon ekzanp rapide itilize LINQ pou analyser kèk  XML Et obtenir nan likè, dous attributs qui.

Se isit la mwen vle analyser XML a:

<?xml vèsyon pipiti de youn = "1.0" codage = "utf 8" ?>
<DeafultConfigurationSets>
  <PageLayouts>

    <Non PageLayout = "xyzzy">
      <Onglets>
        <Onglet TabOrder = "1" TabLabel = "x" SharePointContentType = "yon" AdditionalConstraints = "ac1" />
        <Onglet TabOrder = "2" TabLabel = "y" SharePointContentType = "b" AdditionalConstraints = "ac2" />
      </Onglets>
    </PageLayout>

    <Non PageLayout = "xyzzy2">
      <Onglets>
        <Onglet TabOrder = "100" TabLabel = "x" SharePointContentType = "yon" AdditionalConstraints = "ac1" />
        <Onglet TabOrder = "101" TabLabel = "y" SharePointContentType = "b" AdditionalConstraints = "ac2" />
        <Onglet TabOrder = "103" TabLabel = "z" SharePointContentType = "c" AdditionalConstraints = "ac3" />
      </Onglets>
    </PageLayout>

  </PageLayouts>
</DeafultConfigurationSets>

Mwen vle pou kèk onglets nan D' te konstwi pa l' a CI-dessus.  M' onglets depann sou yon paj en.  Si non M' en paj se "xyzzy2" lè sa a, mwen vle pou jwenn onglets 100, 101 Et 103 (les onglets nan <Non PageLayout = "xyzzy2">).

Isit la se LINQ ki fè li:

var allTabs =
              de p i nan
                  XElement.Parse(theXmlToParse).
                 
Eleman yo("PageLayouts").
                  Eleman yo("PageLayout")
              Kote (p.Attribute("non").Value.Equals("xyzzy2"))
              depi m nan p.Elements("Onglets").Eleman yo("Onglet")
              fè chwa m;

          Results.Text = chaîne.Vid;

          foreach (var aTab nan allTabs)
          {
              Results.Text =
                  "Onglet lòd: " + aTab.Attribute("TabOrder").Valè + " |" +
                  "Onglet etikèt ki: " + aTab.Attribute("TabLabel").Valè + " | " +
                  "SharePointContentType: " + aTab.Attribute("SharePointContentType").Valè + " | " +
                  "AdditionalConstraints: " + aTab.Attribute("AdditionalConstraints").Valè + "r";
          }

Nan CI-dessus a, a varyab "theXmlToParse", ki se yon varyab chaîne, Men, ou ka sèvi ak chay() metòd sou yon ravin si ou renmen.

"Rezilta", ki se yon etikèt ki sou yon fòm web Et lè sa a, kòd s', li sanble ke sa:

SNAGHTML11cd2e7c

Mwen pa te jwenn kijan pou Trier rezilta encore, Se poutèt sa, m' ap kite sa pou yon pwochen blog pòs.

</fen>

Vous pou m' blog.

Swiv mwen sou Twitter nan http://www.twitter.com/pagalvin