Read from xml files with or without a namespace using XmlDocument(使用 XmlDocument 从有或没有命名空间的 xml 文件中读取)
问题描述
我有一些代码使用 XmlDocument 从具有命名空间的 xml 文件中读取.我的挑战是我现在正在读取硬编码的文件的命名空间,我将它传递给 XmlNamespaceManager.我想为我的方法更灵活一点.从任何类型的 xml 文件中读取.如果它有命名空间,则使用命名空间管理器来读取元素,而无需对命名空间进行硬编码.如果文件没有命名空间,然后去前进,只需解析它.下面是我所做的.
I have some code that reads from xml files with a namespace using XmlDocument.My challenge is that i have the namespace of the file i'm reading hard coded for now and i pass that to the XmlNamespaceManager.I would like for my approach to be a little more flexible.To read from any kind of xml file.If it has a namespace,then use the namespace manager to read the elements without hard coding the namespace.If the file doesn't have a namespace,then go ahead and just parse it.Below is what I've done.
xmldoc = new XmlDocument ();
xmldoc.Load (fileLocation);
XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(xmldoc.NameTable);
nameSpaceManager.AddNamespace ("ns","http://schemas.sample.data.org/2005");
XmlNodeList nodeList = xmldoc.SelectNodes("/ns:Demo/ns:Items", nameSpaceManager);
if (nodeList != null)
{
foreach (XmlNode childNode in nodeList)
{
string first = childNode.SelectSingleNode ("ns:First", nameSpaceManager).InnerText;
string second= childNode.SelectSingleNode ("ns:Second", nameSpaceManager).InnerText;
string third = childNode.SelectSingleNode ("ns:Third", nameSpaceManager).InnerText;
}
}
这是我正在使用的示例 xml 文件
Here's the sample xml file i'm using
<Demo xmlns:i="http://www.justasample.com" xmlns="http://schemas.sample.data.org/2005">
<Items>
<First>first</First>
<Second>second</Second>
<Third>third</Third>
</Items>
</Demo>
推荐答案
您可以考虑以下选项:
- 确定文档是否包含命名空间,并根据它构造 xpath 查询
- 使用与命名空间无关的 xpath,例如
local-name()
,这将忽略命名空间
选项 1
var xmlDoc = new XmlDocument();
xmlDoc.Load(fileLocation);
//determine whether document contains namespace
var namespaceName = "ns";
var namespacePrefix = string.Empty;
XmlNamespaceManager nameSpaceManager = null;
if (xmlDoc.FirstChild.Attributes != null)
{
var xmlns = xmlDoc.FirstChild.Attributes["xmlns"];
if (xmlns != null)
{
nameSpaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
nameSpaceManager.AddNamespace(namespaceName, xmlns.Value);
namespacePrefix = namespaceName + ":";
}
}
XmlNodeList nodeList = xmlDoc.SelectNodes(string.Format("/{0}Demo/{0}Items",namespacePrefix), nameSpaceManager);
if (nodeList != null)
{
foreach (XmlNode childNode in nodeList)
{
string first = childNode.SelectSingleNode(namespacePrefix + "First", nameSpaceManager).InnerText;
string second = childNode.SelectSingleNode(namespacePrefix + "Second", nameSpaceManager).InnerText;
string third = childNode.SelectSingleNode(namespacePrefix + "Third", nameSpaceManager).InnerText;
}
}
选项 2
XmlNodeList nodeList = xmlDoc.SelectNodes("/*[local-name() = 'Demo']/*[local-name() = 'Items']");
if (nodeList != null)
{
foreach (XmlNode childNode in nodeList)
{
string first = childNode.SelectSingleNode("*[local-name() = 'First']").InnerText;
string second = childNode.SelectSingleNode("*[local-name() = 'Second']").InnerText;
string third = childNode.SelectSingleNode("*[local-name() = 'Third']").InnerText;
}
}
这篇关于使用 XmlDocument 从有或没有命名空间的 xml 文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 XmlDocument 从有或没有命名空间的 xml 文件中读取


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01