如何从XDocument获得可命名的?

时间:2022-04-25 20:24:23

How do I get a NameTable from an XDocument?

如何从XDocument获得可命名的?

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

它似乎没有XmlDocument所具有的可命名属性。

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检索现有的nametable。

3 个解决方案

#1


28  

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

您需要将XML插入XmlReader,并使用XmlReader的可命名属性。

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的内容中运行),您就可以检索可命名的。

#2


20  

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

马特

#3


4  

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检索现有的nametable。

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

#1


28  

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

您需要将XML插入XmlReader,并使用XmlReader的可命名属性。

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的内容中运行),您就可以检索可命名的。

#2


20  

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

马特

#3


4  

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检索现有的nametable。

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