How do I get a NameTable from an XDocument?

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

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;

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.


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