簡體   English   中英

如何使用C#從當前xml節點列表中讀取子節點值?

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

我瀏覽了許多示例,但找不到正確的代碼。 我有以下很長的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>

這是代碼的一部分,在imageList下,我有以下nodeList。 我使用以下代碼提取了節點列表:

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);
                }
            }

在這段代碼中,我可以讀取節點名稱的值,但是x和y失敗,因為它們是子節點。 在這里閱讀它們的最佳方法是什么?

這里是。

            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