【发布时间】:2018-02-22 00:35:03
【问题描述】:
如何解析xml文件?
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>link</loc>
<lastmod>2011-08-17T08:23:17+00:00</lastmod>
</sitemap>
<sitemap>
<loc>link</loc>
<lastmod>2011-08-18T08:23:17+00:00</lastmod>
</sitemap>
</sitemapindex>
我是 XML 新手,我试过这个,但它似乎不起作用:
XmlDocument xml = new XmlDocument(); //* create an xml document object.
xml.Load("sitemap.xml");
XmlNodeList xnList = xml.SelectNodes("/sitemapindex/sitemap");
foreach (XmlNode xn in xnList)
{
String loc= xn["loc"].InnerText;
String lastmod= xn["lastmod"].InnerText;
}
【问题讨论】:
-
它不起作用怎么办?它是否成功加载文件?如果找到两个站点地图元素吗?它是否读取任何一个站点地图子元素?我假设元素标签之前的那些破折号不在实际的 xml 文件中,对吧?
-
xnList 没有被填充。 (对不起破折号 - 没有破折号)