How to get NameTable from XDocument?

How to get NameTable from XDocument?

It does not have a NameTable property that has an XmlDocument.

EDIT: Judging by the lack of an answer, I guess I might miss the point.

I am making XPath requests against XDocument like this ...

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

It works fine, but I need to manually add the namespaces that I want to use in the XmlNamespaceManager, instead of retrieving the existing sets from the XDocument, as if you were with an XmlDocument.

+34
c # xml xpath linq-to-xml
Jun 01 '09 at 11:43
source share
3 answers

You need to move the XML through the XmlReader and use the XmlReader NameTable property.

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

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

If you create Xml from scratch using XDocument, you will need to call the XDocument CreateReader method, after which you will have something to consume the reader. After the reader is used (say, load another XDocument, but it would be better if someone did nothing to make the reader run the contents of the XDocument), you can get the name TableTable.

+25
Jun 01 '09 at 13:01
source share

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

+20
Oct 20 '10 at 12:36
source share

I need to manually add the namespaces that I want to use for the XmlNamespaceManager instead of retrieving the existing nametable from the XDocument, as with the XmlDocument.

 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(); 
+3
Nov 01 '14 at 9:29
source share



All Articles