简体   繁体   中英

How to read child node value from current xml node list using C#?

I went through many examples but could not find the right code. I have following long xml page.

        <Image>
          <Name>46594867_1.jpg</Name>
          <Style>InspectionNonAnnotated</Style>
          <PixelSize>
            <x>4.7355760022126594</x>
            <y>4.7362810178726678</y>
          </PixelSize>
          <ImageROI>
            <x>243.54022216796875</x>
            <y>245.4705810546875</y>
            <Height>8.75</Height>
            <Width>11.25</Width>
          </ImageROI>
        </Image>
        <Image>
          <Name>46594867_2.jpg</Name>
          <Style>InspectionStandardDeviation</Style>
          <PixelSize>
            <x>4.7355760022126594</x>
            <y>4.7362810178726678</y>
          </PixelSize>
          <ImageROI>
            <x>243.54022216796875</x>
            <y>245.4705810546875</y>
            <Height>8.75</Height>
            <Width>11.25</Width>
          </ImageROI>
        </Image>
        <Image>
          <Name>46594867_3.jpg</Name>
          <Style>InspectionReference</Style>
          <PixelSize>
            <x>4.7355760022126594</x>
            <y>4.7362810178726678</y>
          </PixelSize>
          <ImageROI>
            <x>243.54022216796875</x>
            <y>245.4705810546875</y>
            <Height>8.75</Height>
            <Width>11.25</Width>
          </ImageROI>
        </Image>

This is part of code and under imageList, I have the following nodeList. I extracted the nodelist using the following code :

nodeList = xdoc.GetElementsByTagName("Image");
            {
                foreach (XmlNode node in nodeList)
                {
                    xmlCordinates cordinates = new xmlCordinates();
                    cordinates.fileName = node["Name"].InnerText;
                    cordinates.X = Convert.ToDouble(node["ImageROI/x"].InnerText);
                    cordinates.Y = Convert.ToDouble(node["ImageROI/y"].InnerText);
                    ListCordintes.Add(cordinates);
                }
            }

In this code, I could read the value for node Name, but failed for x and y since they are child nodes. What is the best way to read them here ?

Here it is.

            xmlCordinates cordinates = new xmlCordinates();
            cordinates.fileName = node["Name"].InnerText;
            cordinates.X = Convert.ToDouble(**node.ChildNodes[2]["x"].InnerText**);
            cordinates.Y = Convert.ToDouble(**node.ChildNodes[2]["y"].InnerText**);
            ListCordintes.Add(cordinates);

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