简体   繁体   中英

How can I get the value of a xsi:noNamespaceSchemaLocation attribute using XDocument C#

I have lots of XML files which have various xsi:noNamespaceSchemaLocation values. Thse are in attributes in the root node of the documents. I need to read the xsi:noNamespaceSchemaLocation values from the files. This shouldn't be difficult but I'm struggling.

Example XML (this is the root node):

<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:dc="http://www.purl.org/dc/elements/1.1/" 
xmlns:xlink="http://www.w3.org/1999/xlink" 
xsi:noNamespaceSchemaLocation="http://www.someSchemaLocation.xsd">

It's the value of xsi:noNamespaceSchemaLocation that i'm looking for.

I can do :

string blah = myXdocument.Root.GetNamespaceOfPrefix("xsi").NamespaceName;

and this returns "http://www.w3.org/2001/XMLSchema-instance" as you'd expect.

I've tried:

string test = myXdocument.Root.Attribute("xsi" + "noNamespaceSchemaLocation").Value.ToString(); 

but that returns null.

Any idea please?

Cast blah to XNamespace :

string test = myXdocument.Root.Attribute((XNamespace)blah + "noNamespaceSchemaLocation")
    .Value.ToString();

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