[英]Pull specific xml tag in a xml file using XmlDocument
I have an xml file, what I am trying to do is to parse the complete file and search for a specific xml tag (in my case I am searching for queryString
) and when the tag is encountered pull out the inner text corresponding to it.我有一个 xml 文件,我想要做的是解析完整的文件并搜索特定的 xml 标签(在我的情况下我正在搜索
queryString
),当遇到标签时,拉出与其对应的内部文本。 I am using XmlDocument
and using XmlDocument.SelectNodes("/stringList")
.我正在使用
XmlDocument
并使用XmlDocument.SelectNodes("/stringList")
。
While doing so a null
value is being returned.这样做时会返回一个
null
值。 Am I missing out on something?我错过了什么吗?
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");
}
While execution it does not enter the for loop as xml_nodes
value is null
执行时它不会进入 for 循环,因为
xml_nodes
值为null
Xml File looks like this. Xml 文件看起来像这样。
<stringList>
<property1/>
<property2/>
<style>
<queryString>
</queryString>
</style>
<queryString>
</queryString>
</stringList>
If you're searching only for "queryString" tag I suggest you to use XmlDocument method GetElementsByTagName.如果您只搜索“queryString”标签,我建议您使用 XmlDocument 方法 GetElementsByTagName。 Consider:
考虑:
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);
}
}
}
You will get exactly two nodes you're searching for:您将获得您正在搜索的两个节点:
Who's on first.
Who's on second.
Press any key to continue . . .
I prefer the XML classes/functionality found in System.Xml.Linq :我更喜欢System.Xml.Linq 中的 XML 类/功能:
XDocument doc = XDocument.Parse(xmlString);
foreach (XElement queryString in doc.Descendants("queryString"))
{
// do something with queryString.Value ...
}
you can use xml linq like this你可以像这样使用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.