简体   繁体   中英

Question on xmlns for Xml file in C#

I post the question for I don't understand the keywords of xmlns , xmlns:xsd and xmlns:xsi .

I find an example for XpathNavigator MoveToChile method as this, attached the xml file sample as well.

XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");

The sample Xml file from MSDN.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>

Can you please describe the the difference for xmlns , xmlns:xsd and xmlns:xsi . What can I used to replace http://www.contoso.com/books in my case? or I can just insert another xmlns like xmlns="http://www.mycase.com" ?

Or I don't need include the URL simply. a little like this navigator.MoveToChild("bookstore", "");

My xml file as this,

<?xml version="1.0" encoding="UTF-8"?>
<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <License licenseId="" licensePath=""/>

Appreciated for your replies.

Check out this tutorial on XML namespaces .

Basically, you define a prefix for your XML namespace to make it easier to use:

<Equipment 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <License licenseId="" licensePath=""/>
   <xsd:element ..... xsi:nil="true" />

Those elements that belong into the default namespace are probably those that you need the most, so you can define one XML namespace without a prefix - the default namespace:

<Equipment 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.yourcompany.com/your/default/namespace" >
   <License licenseId="" licensePath=""/>
   <xsd:element ..... xsi:nil="true" />

Now, all the elements in your XML without a specific prefix (here: the <License> tag) are part of your default XML namespace.

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