在xml文档中递归查找节点

时间:2017-04-06 06:48:52

标签: c# xml xml-parsing

<TestSuite name="TestSuite1">
  <TestCase name="TestCase1" UID="1" State="1" DataSourceId="1">
    <TestModule name="Recording1" State="Checked" UID="1">
    </TestModule>
    <TestCase name="TestCase3" UID="bde575e2-74dd-4b5b-9d92-c12cc7e6777d" State="Checked" DataSourceId="" />
  </TestCase>
  <TestCase name="TestCase2" UID="06a4df3b-f072-4f70-a5c3-f0f2ea95654c" State="Checked" DataSourceId="" />
  <TestModule name="ds" State="Checked" UID="16b175a7-b286-484d-ba9a-2a5c9f8dd0fc" />
  <TestModule name="hh" State="Checked" UID="581f5d85-a777-483b-9b5d-ae2830302878" />
</TestSuite>

XmlNode rootNode = xDoc.SelectSingleNode("/TestSuite");
        string s = "16b175a7-b286-484d-ba9a-2a5c9f8dd0fc";
        XmlNode l_objXmlNode = null;
        foreach (XmlNode node in rootNode.ChildNodes)
        {
            l_objXmlNode = ProcesNode(node, s);
            if (l_objXmlNode != null)
                break;
        }

    private static XmlNode ProcesNode(XmlNode node, string l_selectedNodeUID)
    {
        XmlNode l_objXmlNode = null;
        if (!node.HasChildNodes
            || ((node.ChildNodes.Count == 1) && (node.FirstChild is System.Xml.XmlText)))
        {
            if (node.Attributes["UID"] != null && node.Attributes["UID"].Value == l_selectedNodeUID)
            {
                l_objXmlNode = node;
                return l_objXmlNode;    
            }
        }
        else
        {
            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.Attributes["UID"] != null && child.Attributes["UID"].Value == l_selectedNodeUID)
                {
                    l_objXmlNode = child;
                    break;               
                }
                else
                {
                    return ProcesNode(child, l_selectedNodeUID);
                }
            }
        }

        return l_objXmlNode;
    }'

我必须从xml文件中找到最深的子节点,其UID =&#34; value&#34;因为我已经写了上面的递归代码,但l_objXmlNode总是给我空值。我必须从xml文件中找到最深的子节点,其UID =&#34;值&#34;因为我已经在上面写了递归代码,但l_objXmlNode总是给我空值。

2 个答案:

答案 0 :(得分:1)

您可以使用XPath表达式在XML文档中的任何位置找到UID属性等于特定值的元素:

....
string s = "16b175a7-b286-484d-ba9a-2a5c9f8dd0fc";
XmlNode l_objXmlNode = xDoc.SelectSingleNode(String.Format("//*[@UID='{0}']", s));

或使用LINQ-to-XML:

XDocument doc = XDocument.Load("path to your XML file");
string s = "16b175a7-b286-484d-ba9a-2a5c9f8dd0fc";
XElement l_objXmlNode = doc.Descendants().FirstOrDefault(o => (string)o.Attribute("UID") == s);

答案 1 :(得分:1)

试试这个......

<div class="background-image"></div>
<div class="hero-image"></div>

您将获得以下输出

public static void SearchNodes()
        {
            XDocument doc = XDocument.Load(@"D:\\Test\\Test2.xml");

            var v = from nodes in doc.Descendants("TestCase")
                    where nodes.Attribute("UID").Value == "06a4df3b-f072-4f70-a5c3-f0f2ea95654c"
                    select nodes;

            foreach (var item in v)
            {
                Console.Write(item);    
            }

        }

您可以将期望值作为参数传递给SearchNodes函数。