简体   繁体   中英

how to get the namespaces in the root of xml

I have an xml whose root has namespaces like

 <root version="2.0" xsi:schemaLocation="http://www.sample.org/schemas/2009 http://www.sample1.org/schemas/2009/railML-2.0/railML.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dc="http://xyz/elements/1.1/" xmlns="http://www.abcd.org/schemas/2009">

I am able to retrieve the value of xmlns using

var xdoc = XDocument.Load(XmlToParse);
 Console.WriteLine(xdoc.Root.Name.NamespaceName);

but how can i get the values of other namespaces in root ie value of

xsi
dc
schemaLocation

Before parsing I need to verify these namespaces so i need these values. how to do it? can it be done by linq how?

var q = xdoc.Root.Attributes()
                 .Where(x => x.IsNamespaceDeclaration)
                 .Select(x => new {Prefixes = x.Name.LocalName, ns = x.Value});

EDIT:

More Ways: Get namespaces from an XML Document with XPathDocument and LINQ to XML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM