C# 如何从 XDocument 获取 NameTable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/934486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a NameTable from an XDocument?
提问by Simon Keep
How do I get a NameTable from an XDocument?
如何从 XDocument 获取 NameTable?
It doesn't seem to have the NameTable property that XmlDocument has.
它似乎没有 XmlDocument 具有的 NameTable 属性。
EDIT: Judging by the lack of an answer I'm guessing that I may be missing the point.
编辑:从缺乏答案来看,我猜我可能没有抓住重点。
I am doing XPath queries against an XDocument like this...
我正在对这样的 XDocument 进行 XPath 查询...
document.XPathSelectElements("//xx:Name", namespaceManager);
It works fine but I have to manually add the namespaces I want to use to the XmlNamespaceManager rather than retrieving the existing nametable from the XDocument like you would with an XmlDocument.
它工作正常,但我必须手动将我想要使用的命名空间添加到 XmlNamespaceManager,而不是像使用 XmlDocument 那样从 XDocument 检索现有名称表。
采纳答案by AnthonyWJones
You need to shove the XML through an XmlReader and use the XmlReader's NameTable property.
您需要通过 XmlReader 推送 XML 并使用 XmlReader 的 NameTable 属性。
If you already have Xml you are loading into an XDocument then make sure you use an XmlReader to load the XDocument:-
如果您已经将 Xml 加载到 XDocument 中,请确保使用 XmlReader 加载 XDocument:-
XmlReader reader = new XmlTextReader(someStream);
XDocument doc = XDocument.Load(reader);
XmlNameTable table = reader.NameTable;
If you are building Xml from scratch with XDocument you will need to call XDocument's CreateReader method then have something consume the reader. Once the reader has be used (say loading another XDocument but better would be some do nothing sink which just causes the reader to run through the XDocument's contents) you can retrieve the NameTable.
如果您使用 XDocument 从头开始构建 Xml,您将需要调用 XDocument 的 CreateReader 方法,然后让某些东西消耗阅读器。一旦使用了阅读器(比如加载另一个 XDocument,但最好是一些什么都不做的接收器,它只会导致阅读器运行 XDocument 的内容),您可以检索 NameTable。
回答by Matthew McDermott
I did it like this:
我是这样做的:
//Get the data into the XDoc
XDocument doc = XDocument.Parse(data);
//Grab the reader
var reader = doc.CreateReader();
//Set the root
var root = doc.Root;
//Use the reader NameTable
var namespaceManager = new XmlNamespaceManager(reader.NameTable);
//Add the GeoRSS NS
namespaceManager.AddNamespace("georss", "http://www.georss.org/georss");
//Do something with it
Debug.WriteLine(root.XPathSelectElement("//georss:point", namespaceManager).Value);
Matt
马特
回答by Alex
I have to manually add the namespaces I want to use to the XmlNamespaceManager rather than retrieving the existing nametable from the XDocument like you would with an XmlDocument.
我必须手动将要使用的命名空间添加到 XmlNamespaceManager,而不是像使用 XmlDocument 那样从 XDocument 检索现有名称表。
XDocument project = XDocument.Load(path);
//Or: XDocument project = XDocument.Parse(xml);
var nsMgr = new XmlNamespaceManager(new NameTable());
//Or: var nsMgr = new XmlNamespaceManager(doc.CreateReader().NameTable);
nsMgr.AddNamespace("msproj", "http://schemas.microsoft.com/developer/msbuild/2003");
var itemGroups = project.XPathSelectElements(@"msproj:Project/msproj:ItemGroup", nsMgr).ToList();
回答by Sylwester Santorowski
It can also be done by XPathNavigator. Can be useful when you know neither Xml file Encoding nor namespace prefixes.
它也可以通过 XPathNavigator 来完成。当您既不知道 Xml 文件编码也不知道命名空间前缀时,这很有用。
XDocument xdoc = XDocument.Load(sourceFileName);
XPathNavigator navi = xdoc.Root.CreateNavigator();
XmlNamespaceManager xmlNSM = new XmlNamespaceManager(navi.NameTable);
//Get all the namespaces from navigator
IDictionary<string, string> dict = navi.GetNamespacesInScope(XmlNamespaceScope.All);
//Copy them into Manager
foreach (KeyValuePair<string, string> pair in dict)
{
xmlNSM.AddNamespace(pair.Key, pair.Value);
}