简体   繁体   中英

extracting theNamespace from the xml file

I am working on the XML files in C#.

I want to extact the name space and do some maniplations.

say my xml file looks like this.

        <Content xmlns="http://ABCD.com/sdltridion/schemas/XXXXX">
            <first>ABCD</first>
            <second>DCEF</second>
        </Content>

I want to extract Xml namespace from the root tag, ang get the value of XXXXX.

Output needed: XXXXX

Can any one help regarding this.

Thank you.

Try this:

var xdoc = XDocument.Parse(xml);
var ns = xdoc.Root.Name.Namespace.NamespaceName;
var value = new Uri(ns).Segments.LastOrDefault();

You can try XNamespace class

 XNamespace ns = XNamespace.Get("http://ABCD.com/sdltridion/schemas/XXXXX");
 var result = XElement.Load("URL").Descendants(ns + "NODENAME"); 

Thanks

Deepu

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