繁体   English   中英

使用 XmlDocument 在 xml 文件中提取特定的 xml 标签

[英]Pull specific xml tag in a xml file using XmlDocument

我有一个 xml 文件,我想要做的是解析完整的文件并搜索特定的 xml 标签(在我的情况下我正在搜索queryString ),当遇到标签时,拉出与其对应的内部文本。 我正在使用XmlDocument并使用XmlDocument.SelectNodes("/stringList")

这样做时会返回一个null值。 我错过了什么吗?

XmlDocument xml = new XmlDocument();
Jrxml.Load(file_path);
XmlNodeList xml_nodes = xml.SelectNodes("/stringList");
foreach (XmlNode jr_node in xml_nodes)
{
    XmlNode query_node = jr_node.SelectSingleNode("queryString");
}

执行时它不会进入 for 循环,因为xml_nodes值为null

Xml 文件看起来像这样。

<stringList>
    <property1/>
    <property2/>
       <style>
         <queryString>
         </queryString>
       </style>
    <queryString>
    </queryString>
</stringList>

如果您只搜索“queryString”标签,我建议您使用 XmlDocument 方法 GetElementsByTagName。 考虑:

using System;
using System.Xml;
namespace TestCon
{
    class Program
    {
        private static XmlDocument TestDoc;
        public static void Main(string[] args)
        {
            TestDoc = new XmlDocument();
            TestDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                    "<stringList>\n"+
                    "<property1/>\n"+"<property2/>\n"+
    "<style>\n"+"<queryString>Who's on fist."+"</queryString>\n"+
    "</style>\n"+"<queryString>Who's on second."+"</queryString>\n"+
    "</stringList>");
            XmlNodeList elemList = TestDoc.GetElementsByTagName("queryString");
            foreach (XmlNode foundNode in elemList) 
            {
                Console.WriteLine(foundNode.InnerText);
            }
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

您将获得您正在搜索的两个节点:

Who's on first.
Who's on second.
Press any key to continue . . .

我更喜欢System.Xml.Linq 中的 XML 类/功能:

XDocument doc = XDocument.Parse(xmlString);

foreach (XElement queryString in doc.Descendants("queryString"))
{
    // do something with queryString.Value  ...
}

你可以像这样使用xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<root>" +
                "<stringList>" +
                    "<property1/>" +
                    "<property2/>" +
                       "<style>" +
                         "<queryString>" +
                         "</queryString>" +
                       "</style>" +
                    "<queryString>" +
                    "</queryString>" +
                "</stringList>" +
                "</root>";
            XDocument doc = XDocument.Parse(xml);

            var stringList = doc.Descendants("stringList").Select(x => new
            {
                property1 = x.Element("property1"),
                property2 = x.Element("property2"),
                style = x.Element("style"),
                queryString = x.Element("queryString")
            });

        }
    }
}

暂无
暂无

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

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