这是我的饲料。
<feed xml:lang="">
<title>NEWS.com.au | Top Stories</title>
<link rel="self" href="http://feeds.news.com.au/public/atom/1.0/news_top_stories_48_48.xml"/>
<link rel="alternate" href="http://news.com.au"/>
<id>http://news.com.au</id>
<rights/>
<entry>
<title>F1’s glaring issues exposed</title>
<link href="www.google.com"/>
<author>
<name>STEVE LARKIN</name>
</author>
<link rel="enclosure" type="image/jpeg" length="2373" href="abc.jpg"/>
</entry>
<entry>
.....
</entry>
</feed>
这就是我阅读xml的方式。
string downloadfolder = "C:/Temp/Download/abc.xml";
XmlDocument xml = new XmlDocument();
xml.Load(downloadfolder);
XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
string xpath_title = "atom:feed/atom:entry/atom:title";
XmlNodeList nodes_title = xml.SelectNodes(xpath_title, nsmgr);
foreach (XmlNode node_title in nodes_title)
{
Console.WriteLine(node_title.InnerText);
}
string xpath_author = "atom:feed/atom:entry/atom:author";
XmlNodeList nodes_author = xml.SelectNodes(xpath_author, nsmgr);
foreach (XmlNode node_author in nodes_author)
{
Console.WriteLine(node_author.InnerText);
}
string xpath_link = "atom:feed/atom:entry/atom:link";
XmlNodeList nodes_link = xml.SelectNodes(xpath_link, nsmgr);
foreach (XmlNode node_link in nodes_link)
{
Console.WriteLine(node_link.Attributes["href"].Value);
}
我想阅读<entry>
节点内的标题,链接,作者。我正在定义xpath,然后迭代每个节点的值,是否有其他方法可以一次定义xpath,然后迭代该<entry>
节点的所有值
要在<entry>
node的所有子节点上进行操作,可以在处停止XPath /atom:entry
。然后在循环中,根据需要选择每个子节点,例如:
......
String xpath = "atom:feed/atom:entry";
XmlNodeList nodes2 = xml.SelectNodes(xpath, nsmgr);
foreach (XmlNode node in nodes2)
{
var title = node.SelectSingleNode("./atom:title", nsmgr).InnerText;
var link1 = node.SelectSingleNode("./atom:link[1]", nsmgr).Attributes["href"].Value;
//go on to select and operate on the rest child nodes
//.......
}
注意,您需要.
在XPath的开头添加一个点(),以使XPath上下文相对于当前上下文node
而不是整个XML文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句