使用XmlDocument从具有或不具有名称空间的xml文件读取 [英] Read from xml files with or without a namespace using XmlDocument
问题描述
我有一些代码可以使用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()
),它将 忽略名称空间
- determine whether document contains namespace and depending on it construct xpath query
- use namespace agnostic xpath such as
local-name()
, which will ignore the namespace
选项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文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!