简体   繁体   English

如何使用C#获取xml中下一个节点的值

[英]how to get the value of next node in xml using C#

Here is the xml code: 这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<xd:xmldiff version="1.0" srcDocHash="11928043053884448382" options="IgnoreChildOrder IgnoreNamespaces IgnoreWhitespace IgnoreXmlDecl " fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
    <xd:node match="2">
        <xd:node match="2">
            <xd:node match="19">
                <xd:node match="2">
                    <xd:add>Y</xd:add>
                </xd:node>
            </xd:node>
            <xd:add match="/2/2/11" opid="2" />
            <xd:change match="18" name="OWNINGSITE">
                <xd:node match="2">
                    <xd:remove match="1" />
                </xd:node>
            </xd:change>
            <xd:add match="/2/2/2-9" opid="1" />
            <xd:change match="17" name="STATUS">
                <xd:node match="2">
                    <xd:remove match="1" />
                </xd:node>
            </xd:change>
            <xd:remove match="14-16" />
            <xd:remove match="13" subtree="no">
                <xd:remove match="1-2" />
            </xd:remove>
            <xd:remove match="11" opid="2" />
            <xd:remove match="10" />
            <xd:remove match="2-9" opid="1" />
            <xd:remove match="1" />
        </xd:node>
        <xd:node match="5">
            <xd:node match="3">
                <xd:node match="11">
                    <xd:change match="1">0,1,0,1,0,0,0,0,1</xd:change>
                </xd:node>
            </xd:node>
        </xd:node>
    </xd:node>
    <xd:descriptor opid="1" type="move" />
    <xd:descriptor opid="2" type="move" />
</xd:xmldiff>

I have written the c# code snippet to get the value of "match". 我已经编写了C#代码段以获取“ match”的值。 I am able to get the value of first match ie "2" but am unable to go to the next node and get the value of its "match". 我能够获取第一个匹配的值,即“ 2”,但无法转到下一个节点并获取其“匹配”的值。 My output should be: 我的输出应为:

first match value 2 第一个匹配值2

second match value 2 第二匹配值2

third match value 19 第三匹配值19

    namespace demo
    {
        class Program
        {
            static void Main()
            {

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(@"C:\\shreyas\\NX_Temp\\NX_Temp\\000048_A\\CompareReport3D.xml");
               // XmlNodeList nodeList1= xmlDoc.ReadNode();

                XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("*");
               string[] arr1 = new string[10];
                int i = 0;
               foreach (XmlNode node1 in nodeList)
               {
                   XmlNode personalNode = node1.SelectSingleNode("/node");
                   arr1[i] = node1.Attributes["match"].Value;
                   Console.WriteLine("first match value:" +arr1[i]);
                   i++;
//upto this point am getting the output and the exception after this
                   arr1[i] = personalNode.Attributes["match"].Value;
                   Console.WriteLine("second match value:" + arr1[i]);
                   i++;
                   arr1[i] = personalNode.Attributes["match"].Value;
                   Console.WriteLine("third match value:" + arr1[i]);

               }

            }
        }  
    }

You have to do a recursive call . 您必须进行递归调用

Create a method: 创建一个方法:

 public static void TraverseXml(XmlNodeList nodeList)
    {
        foreach (XmlNode node1 in nodeList)
        {
            if (node1.Attributes!=null && node1.Attributes["match"] != null)
            {
                Console.WriteLine("first match value:" + node1.Attributes["match"].Value);
            }
            TraverseXml(node1.ChildNodes);
        }
        Console.ReadLine();
    }

And from your main method call : 从您的主要方法调用:

static void Main()
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"C:\\shreyas\\NX_Temp\\NX_Temp\\000048_A\\CompareReport3D.xml");
           // XmlNodeList nodeList1= xmlDoc.ReadNode();

            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("*");
            TraverseXml(nodeList);
        }

little fix for correct output information :) 正确输出信息的小修正:)

public static List<XmlNode> TraverseXml(XmlNodeList nodeList, int counter = 1)
    {
        List<XmlNode> matchNode = new List<XmlNode>();


        foreach (XmlNode node1 in nodeList)
        {
            if (node1.Attributes != null && node1.Attributes["match"] != null)
            {

                matchNode.Add(node1.Attributes["match"]);
                Console.WriteLine(counter + " match value:" + node1.Attributes["match"].Value);
                counter++;
            }
            TraverseXml(node1.ChildNodes, counter);
        }
        Console.ReadLine();
        return matchNode;
    }

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

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