C# 在XML中选择节点
我正在尝试使用c中的xpath选择节点 这是我的XML文件C# 在XML中选择节点,c#,xml,xpath,C#,Xml,Xpath,我正在尝试使用c中的xpath选择节点 这是我的XML文件 <?xml version="1.0" encoding="ISO-8859-1"?> <rss version="2.0"> <channel> <title>Some other title</title> <item> <description><![CDATA[<img
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>Some other title</title>
<item>
<description><![CDATA[<img src="http://www.site.com/image.jps"/><br />]]></description>
</item>
<item>
<title>This title</title>
<subtitle><subtitle>
<Date>Fri, 21 Mar 2014 08:30:44 GMT</Date>
<description>Some description</description>
</item>
<item>
<title>The other title</title>
<subtitle><subtitle>
<Date>Fri, 21 Mar 2014 08:30:44 GMT</Date>
<description>The other description</description>
</item>
</channel>
</rss>
我想从以下项目输出标题:
This title
The other title
如果您知道如何执行此操作,请告诉我您只是缺少rss根目录的完整路径: 因为不是所有的s都有标题,所以排除那些没有标题的。 另外,请注意,xml区分大小写:
string fieldLine = xn["title"].InnerText;
您只是缺少rss根目录的完整路径: 因为不是所有的s都有标题,所以排除那些没有标题的。 另外,请注意,xml区分大小写:
string fieldLine = xn["title"].InnerText;
我建议您使用:
或者使用XPath:
var titles = xdoc.XPathSelectElements("rss/channel/item/title")
.Select(t => (string)t);
返回带有标题的IEnumerable
foreach (string title in titles)
Console.WriteLine("Title: {0}", title); // note item placeholder in format
我建议您使用:
或者使用XPath:
var titles = xdoc.XPathSelectElements("rss/channel/item/title")
.Select(t => (string)t);
返回带有标题的IEnumerable
foreach (string title in titles)
Console.WriteLine("Title: {0}", title); // note item placeholder in format
请考虑以下几点,你将得到所需的输出。
1您发布的问题中的子标题缺少结尾标记,请在结尾标记中添加“/” 2您非常接近正确的代码,请将其替换为: XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
XmlNodeList nodeList = doc.SelectNodes("rss/channel/item/title");
foreach (XmlNode xn in nodeList)
{
Console.WriteLine("Title: {0}", xn.InnerText);
}
请考虑以下几点,你将得到所需的输出。
1您发布的问题中的子标题缺少结尾标记,请在结尾标记中添加“/” 2您非常接近正确的代码,请将其替换为: XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
XmlNodeList nodeList = doc.SelectNodes("rss/channel/item/title");
foreach (XmlNode xn in nodeList)
{
Console.WriteLine("Title: {0}", xn.InnerText);
}
请不要在问题标题中包含有关语言的信息,除非没有它就没有意义。标记用于此目的。字幕元素在xml中不闭合。我相信这只是一个小问题typo@OndrejJanacek好啊thanks@SergeyBerezovskiy是的。谢谢,不要在问题标题中包含关于所用语言的信息,除非没有它就没有意义。标记用于此目的。字幕元素在xml中不闭合。我相信这只是一个小问题typo@OndrejJanacek好啊thanks@SergeyBerezovskiy是的。谢谢