获得从XML文件节点 [英] Get nodes from xml files

查看:126
本文介绍了获得从XML文件节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析xml文件?

 <?XML版本=1.0编码=UTF-8>?; 
< sitemapindex的xmlns =htt​​p://www.sitemaps.org/schemas/sitemap/0.9>
<站点地图与GT;
< LOC>链接< / LOC>
<&的lastmod GT; 2011-08-17T08:23:17 + 00:00< / - 的lastmod GT;
< /&地图GT;
<站点地图与GT;
< LOC>链接< / LOC>
<&的lastmod GT; 2011-08-18T08:23:17 + 00:00< / - 的lastmod GT;
< /&地图GT;
< / sitemapindex>



我是新来的XML,我想这一点,但它似乎不工作:

  XmlDocument的XML =新的XmlDocument(); // *创建一个XML文档对象。 
与XML.load(sitemap.xml的);
XmlNodeList中xnList = xml.SelectNodes(/ sitemapindex /网站导航)​​;
的foreach(在xnList XmlNode的XN)
{
串LOC-XN [禄]的InnerText。
字符串的lastmod = XN [的lastmod]的InnerText。
}


解决方案

的问题是,在 sitemapindex 元素定义了默认的命名空间。您需要在您选择的节点指定命名空间,否则将无法找到他们。例如:

  XmlDocument的XML =新的XmlDocument(); 
与XML.load(sitemap.xml的);
的XmlNamespaceManager经理=新的XmlNamespaceManager(xml.NameTable);
manager.AddNamespace(S,http://www.sitemaps.org/schemas/sitemap/0.9);
XmlNodeList中xnList = xml.SelectNodes(/ S:sitemapindex / S:网站导航,经理);



通常来讲,当使用的XmlNamespaceManager ,你可以离开前缀为空字符串指定您希望该命名空间是默认的命名空间。所以,你会认为你能够做这样的事:

  //将无法正常工作
的XmlDocument XML =新的XmlDocument();
与XML.load(sitemap.xml的);
的XmlNamespaceManager经理=新的XmlNamespaceManager(xml.NameTable);
manager.AddNamespace(,http://www.sitemaps.org/schemas/sitemap/0.9); //空前缀
XmlNodeList中xnList = xml.SelectNodes(/ sitemapindex /网站导航,经理); // XPath中
无前缀



然而,如果你尝试代码,你会发现,它赢得找不到任何匹配的节点。这样做的原因是,在的XPath 1.0(这是XmlDocument的实现),当没有提供空间,它总是使用空空间,而不是在默认的命名空间。所以,如果你指定要在的XmlNamespaceManager ,它不会默认命名空间由XPath的使用,反正也无所谓。引述从官方XPath规范有关段落:




的QName在节点测试扩展到使用
命名空间中的声明,表达语境的扩展名。这是相同的
的方式扩展在开始元素类型名称和结束标记
除了与的xmlns声明的默认名称空间不使用完成:如果
的QName不有前缀,则命名空间URI为空
(此
是同样的属性名被扩展)。这是一个错误,如果
的QName有前缀其中有在
表达式中没有命名空间声明。




因此,当你在读属于命名空间的元素,你无法避免将命名空间前缀在你的XPath语句。但是,如果你不想打扰把命名空间URI在你的代码,你可以使用的XmlDocument 对象返回根元素的URI,在这个情况下,是你想要什么。例如:

  XmlDocument的XML =新的XmlDocument(); 
与XML.load(sitemap.xml的);
的XmlNamespaceManager经理=新的XmlNamespaceManager(xml.NameTable);
manager.AddNamespace(S,xml.DocumentElement.NamespaceURI); //使用XML的属性,而不是硬编码的URI
XmlNodeList中xnList = xml.SelectNodes(/ S:sitemapindex / S:网站导航,经理);


How to parse the xml file?

<?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>

I am new to XML, I tried this, but it seems to be not working :

        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;
        }

解决方案

The problem is that the sitemapindex element defines a default namespace. You need to specify the namespace when you select the nodes, otherwise it will not find them. For instance:

XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("s", "http://www.sitemaps.org/schemas/sitemap/0.9");
XmlNodeList xnList = xml.SelectNodes("/s:sitemapindex/s:sitemap", manager);

Normally speaking, when using the XmlNameSpaceManager, you could leave the prefix as an empty string to specify that you want that namespace to be the default namespace. So you would think you'd be able to do something like this:

// WON'T WORK
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("", "http://www.sitemaps.org/schemas/sitemap/0.9"); //Empty prefix
XmlNodeList xnList = xml.SelectNodes("/sitemapindex/sitemap", manager); //No prefixes in XPath

However, if you try that code, you'll find that it won't find any matching nodes. The reason for this is that in XPath 1.0 (which is what XmlDocument implements), when no namespace is provided, it always uses the null namespace, not the default namespace. So, it doesn't matter if you specify a default namespace in the XmlNamespaceManager, it's not going to be used by XPath, anyway. To quote the relevant paragraph from the Official XPath Specification:

A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error if the QName has a prefix for which there is no namespace declaration in the expression context.

Therefore, when the elements you are reading belong to a namespace, you can't avoid putting the namespace prefix in your XPath statements. However, if you don't want to bother putting the namespace URI in your code, you can just use the XmlDocument object to return the URI of the root element, which in this case, is what you want. For instance:

XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("s", xml.DocumentElement.NamespaceURI); //Using xml's properties instead of hard-coded URI
XmlNodeList xnList = xml.SelectNodes("/s:sitemapindex/s:sitemap", manager);

这篇关于获得从XML文件节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆