Reputation: 14888
Given an XElement
loaded with
var root = XElement.Load("foo.xml");
How can you enumerate the namespaces used in the XML file?
Upvotes: 0
Views: 83
Reputation: 244878
There is nothing that gives you all the namespaces used directly, so you have to enumerate all elements and extract their namespaces:
var namespaces = root.DescendantsAndSelf()
.Select(e => e.Name.Namespace)
.Distinct();
This would give you only the namespaces that were used by elements in the document. If you want to list all namespaces that are declared in the document (even if they are not used or if they are used only by attributes), you would need to iterate the namespace declarations:
var namespaces = root.DescendantsAndSelf()
.Attributes()
.Where(a => a.IsNamespaceDeclaration)
.Select(a => (XNamespace)a.Value)
.Distinct();
Upvotes: 1