Want to use XPath with LINQ to XML on files with lots namespaces, like InfoPath forms?
This might help you :)
// Add all namespaces on your document public static class MyExtensions { public static XmlNamespaceManager CreateNamespaceManager(this XDocument doc) { XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.CreateReader().NameTable); foreach (XAttribute attr in doc.Root.Attributes()) { if (attr.IsNamespaceDeclaration) nsmgr.AddNamespace(attr.Name.LocalName, attr.Value); } return nsmgr; } }
Then query your XDocument using the System.Xml.XPath extensions:
XDocForm.XPathSelectElement("/my:Field1/my:Field2/my:Level1", XmlForm.CreateNamespaceManager());