简体   繁体   English

如何使用C#从当前xml节点列表中读取子节点值?

[英]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. 我有以下很长的xml页面。

        <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. 这是代码的一部分,在imageList下,我有以下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. 在这段代码中,我可以读取节点名称的值,但是x和y失败,因为它们是子节点。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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