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

时间:2016-01-13 05:40:31

标签: c# .net xml

这是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>

我已经编写了c#代码段来获取“匹配”的值。我能够获得第一个匹配的值,即“2”但是无法进入下一个节点并获得其“匹配”的值。 我的输出应该是:

第一个匹配值2

第二场比赛值2

第三场比赛值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]);

               }

            }
        }  
    }

2 个答案:

答案 0 :(得分:0)

您必须执行递归调用

创建方法:

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

从您的主要方法调用:

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

答案 1 :(得分:0)

对正确输出信息的一点修复:)

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