简体   繁体   中英

How do I read an xml node child that has same name as parent in c#

I am new to XML. I am trying to read an XML node that has an unknown number of children, all with the same name. The parent and children each have a value. I want to place the value in to a list of strings, in the same order as the nodes.

Reading the data reads all of the nodes, including the childrens, inner text and ads it as one entry

then my loop doing this causes an exception on the last iteration through the loop

I created the XML file and tried making a while loop to read the value, then reassign the parent node to the child node and repeat until there are no more children.

In the loop I wrote only the child data is displayed, skipping the parent

XML snippet

    <DataEntry>
        <Key>1001</Key>
        <Default>test 1</Default>
        <Notes>comment 1</Notes>
        <Path>
          AAAAA
          <Path>
            BBBBB
          </Path>
        </Path>
    </DataEntry>
    <DataEntry>
        <Key>1002</Key>
        <Default>test 1</Default>
        <Notes>comment 1</Notes>
        <Path>
          AAAAA
          <Path>
            BBBBB
            <Path>
              CCCCC
              <Path>
                DDDDD
              </Path>
            </Path>
          </Path>
        </Path>
    </DataEntry>

C# Code:

...                        
                        List<string> dataPath = new List<string>();
                        XmlNode pth = xmlNode.SelectSingleNode("Path");
                        while (pth != null)
                        {
                            dataPath.Add(pth["Path"].InnerText);
                            pth = pth.SelectSingleNode("Path");
                        }
...

DataPath looks like:

1st Data entry
dataPath[0] = BBBBB

2nd Data entry
dataPath[0] = BBBBB     CCCCC     DDDDD
dataPath[1] = CCCCC     DDDDD
dataPath[2] = DDDDD

What I want:

dataPath[0] = AAAAA
dataPath[1] = BBBBB
dataPath[2] = CCCCC
dataPath[3] = DDDDD

What do I need to change to get the output I am looking for?

You could use a recursive function like:

      public static List<string> GetPathValues(XmlNode node)
      {
         List<string> dataPath = new List<string>();
         XmlNode pathNode = node.SelectSingleNode("Path");
         if (pathNode != null)
         {
            dataPath.Add(pathNode.ChildNodes.OfType<XmlText>().Single().Value.Trim());
            dataPath.AddRange(GetPathValues(pathNode));
         }
         return dataPath;
      }

call the method with the DataEntry XmlNode as parameter

After some more searching I found the "FirstChild" variable, I changed my to:

List<string> dataPath = new List<string>();
XmlNode pathNode = node.SelectSingleNode("Path");
while (pth != null)
{
    dataPath.Add(pathNode.FirstChild.InnerText.Trim());
    pathNode = pathNode.SelectSingleNode("Path");
}                 

And it is working well.

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