我如何从一个XDocument一个NameTable? [英] How do I get a NameTable from an XDocument?

查看:229
本文介绍了我如何从一个XDocument一个NameTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个XDocument得到NameTable?

How do I get a NameTable from an XDocument?

这似乎不具有的XmlDocument有NameTable属性。

It doesn't seem to have the NameTable property that XmlDocument has.

编辑:由于缺乏一个答案我猜我可能缺少点看

Judging by the lack of an answer I'm guessing that I may be missing the point.

我做对一个XPath查询的XDocument像这样...

I am doing XPath queries against an XDocument like this...

document.XPathSelectElements("//xx:Name", namespaceManager);



它工作正常,但我必须手动添加我想使用XmlNamespaceManager的,而不是获取的命名空间从的XDocument现有nametable就像你使用一个XmlDocument。

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.

推荐答案

您需要通过一个XmlReader推去的XML和使用。XmlReader中的NameTable属性。

You need to shove the XML through an XmlReader and use the XmlReader's NameTable property.

如果您已经有要装载到一个XDocument xml,那么一定要使用一个XmlReader加载的XDocument: -

If you already have Xml you are loading into an XDocument then make sure you use an XmlReader to load the XDocument:-

XmlReader reader = new XmlTextReader(someStream);
XDocument doc = XDocument.Load(reader);
XmlNameTable table = reader.NameTable;

如果您是从头开始构建的Xml用的XDocument你需要调用然后的XDocument的CreateReader方法有一些消耗读者。一旦读者已经可以使用(比如加载另一个的XDocument,但更好的是一些什么都不做水槽刚刚引起读者通过的XDocument的内容运行),您可以检索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一个NameTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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