简体   繁体   中英

Select nodes attribute in XML file using C#

In the following XML file, i try to save in a list all the id attributes:

<?xml version="1.0"?>
<PressReleases>
  <PressRelease id="545" version="1">
    <Name>Convert number to string</Name>
    <Date>20/05/1985</Date>
    <Input>1</Input>
    <Output>One</Output>
  </PressRelease>
  <PressRelease id="544" version="1">
    <Name>Find succeeding characters</Name>
    <Date>19/05/1985</Date>
    <Input>abc</Input>
    <Output>def</Output>
  </PressRelease>
  <PressRelease id="543" version="1">
    <Name>Convert multiple numbers to strings</Name>
    <Date>17/05/1985</Date>
    <Input>123</Input>
    <Output>One Two Three</Output>
  </PressRelease>
  <PressRelease id="542" version="1">
    <Name>Find correlated key</Name>
    <Date>02/05/1985</Date>
    <Input>a1</Input>
    <Output>b1</Output>
  </PressRelease>
  <PressRelease id="541" version="1">
    <Name>Count characters</Name>
    <Date>04/02/1985</Date>
    <Input>This is a test</Input>
    <Output>14</Output>
  </PressRelease>
  <PressRelease id="540" version="1">
    <Name>Another Test</Name>
    <Date>09/01/1985</Date>
    <Input>Test Input</Input>
    <Output>10</Output>
  </PressRelease>
</PressReleases>

This block of code saves only the first Id written in the first Press Release node (545). I need all of them (545,544 ect)

    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\Users\ARNAUDR\Desktop\test.xml");
    //string xmlcontents = doc.InnerXml;

    XmlNodeList xnList = doc.SelectNodes("/PressReleases");
    List<int> IDsInDistantXML = new List<int>();
    foreach (XmlNode xn in xnList)
    {
        XmlNode PressRelease = xn.SelectSingleNode("PressRelease");
        if (PressRelease != null)
        {
            IDsInDistantXML.Add(Convert.ToInt16(PressRelease.Attributes["id"].Value));
        }
    }

Thanks in advance for your help

I suggest you to use Linq to Xml:

XDocument xdoc = XDocument.Load(@"C:\Users\ARNAUDR\Desktop\test.xml");
List<int> ids = xdoc.Root.Elements("PressRelease")
                    .Select(pr => (int)pr.Attribute("id"))
                    .ToList();

Note: your problem is that you always select first (single node) PressRelease node from root, and you are enumerating root nodes (which is always one) instead of enumerating PressRelease nodes. Here is fixed solution:

XmlNodeList pressReleases = doc.SelectNodes("/PressReleases/PressRelease");
List<int> IDsInDistantXML = new List<int>();
foreach (XmlNode pr in pressReleases)    
    IDsInDistantXML.Add(Convert.ToInt32(pr.Attributes["id"].Value));    

Alternatively, if all you need is the one attribute, change your SelectNodes xpath directly to the nodes:

List<int> IDsInDistantXML = new List<int>();
foreach (var xn in doc.SelectNodes("/PressReleases/PressRelease"))
{
    IDsInDistantXML.Add(Convert.ToInt16(xn.Attributes["id"].Value));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM