c# - 从 xml 文件中获取节点

标签 c# xml winforms

如何解析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;
        }

最佳答案

问题在于 sitemapindex 元素定义了默认命名空间。选择节点时需要指定命名空间,否则将找不到它们。例如:

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

通常来说,在使用XmlNameSpaceManager 时,您可以将前缀保留为空字符串,以指定您希望该 namespace 成为默认 namespace 。所以你会认为你可以做这样的事情:

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

但是,如果您尝试该代码,您会发现它找不到任何匹配的节点。原因是在 XPath 1.0(这是 XmlDocument 实现的)中,当没有提供命名空间时,它总是使用 null 命名空间,而不是默认命名空间。因此,无论您是否在 XmlNamespaceManager 中指定默认 namespace 都没有关系,无论如何 XPath 都不会使用它。引用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.

因此,当您正在读取的元素属于一个 namespace 时,您无法避免将 namespace 前缀放在您的 XPath 语句中。但是,如果您不想在代码中放入命名空间 URI,您可以只使用 XmlDocument 对象返回根元素的 URI,在本例中,这就是您想要的.例如:

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

关于c# - 从 xml 文件中获取节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11348229/

相关文章:

c# - 为什么我的汉字在c#字符串中显示不正确

c# - 如何继承基本文本框以提供彩色边框

在 Form_FormClosing 事件中设置 e.Cancel = true 后,c# WinForms 表单仍然关闭

.net - WPF 控件未显示在 WinForms 应用程序的 ElementHost 中

c# - 我如何将 AllowHtml 属性与 Entity Framework 一起使用

c# - 如何将数据从网络表单页面发布到 HTTPHandler.ashx 文件?

android - 使用一张图像表示不同的按钮状态

python - 使用 lxml 解析具有嵌套命名空间的 XML 属性

java - 在 Anypoint Studio (MuleSoft) 中发送电子邮件时出错

c# - System.Web.HttpContext.Current.Cache 信息