如何根据我的要求使用 C# 从 XML 节点读取

本文关键字: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# 代码来解析每个节点并获取匹配值。我无法弄清楚如何解析节点以获取所有匹配项的值。在调试代码时,我发现nodelist没有通过"xmlDoc.DocumentElement.SelectNodes("/node"(;"接收适当的值,因此foreach根本没有被执行。我对"/node"的使用是否正确地获取其属性"match"的值?

namespace demo
{
    class Program
    {
        static void Main()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("C:''shreyas''NX_Temp''NX_Temp''000048_A''CompareReport3D.xml");
            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/node");
            string[] arr1 = new string[10];
            int i = 0;
            foreach (XmlNode node in nodeList)
            {
                 arr1[i] = node.Attributes["match"].Value;
                 i++;
            }
            i = 0;
            while (arr1[i] != null)
            {
                Console.WriteLine("the String=" + arr1[i]+ ":" + arr1[i++]+ ":" + arr1[i++]+":" + arr1[i++]);
            }
        }
    }
}

我需要以字符串的形式找到"匹配"的值

输出应为: the String = 2:2:19:2

如何根据我的要求使用 C# 从 XML 节点读取

最简单的方法是使用递归。

编写一个接受XmlNode nodestring currentPathList<string> paths的函数。它应该:

  1. nodematch 属性追加到currentPath,并检查node是否具有node s 的子项。
  2. 如果没有孩子,它会currentPath添加到paths并返回。
  3. 如果有子项
  4. ,则通过传递子项、currentPath(已修改(和paths来调用每个子项。

首先,您将使用根节点、空字符串和空列表调用此函数。函数完成后,以前的空列表将包含您的路径,例如示例的两个路径:

    "2:2:
  • 19:2">
  • "2:5:3:11">

您可以使用正则表达式来匹配您要查找的所有属性:

// Find all attributes that match the pattern match="x" - where x can be any value.
foreach (var match in Regex.Matches(str, "match='"([^'"]+)'""))
{
    // For each match, match only the "x" part of the result and remove the containing ".
    Console.WriteLine(Regex.Match(match.ToString(), "'"([^'"]+)'"").ToString()
        .Replace("'"", string.Empty));
}