如何使用C#获取xml中next节点的值

本文关键字:next 节点 xml 何使用 获取 | 更新日期: 2023-09-27 18:30:01

这是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#代码片段来获得"match"的值。我能够获得第一个匹配的值,即"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]);
               }
            }
        }  
    }

如何使用C#获取xml中next节点的值

您必须执行递归调用

创建一个方法:

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

正确输出信息的小修复:)

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