HighTechTalks DotNet Forums  

Adding a second namespace declaration?

Dotnet XML microsoft.public.dotnet.xml


Discuss Adding a second namespace declaration? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
David Veeneman
 
Posts: n/a

Default Adding a second namespace declaration? - 11-09-2007 , 07:43 PM






How do I add a second namespace declaration to the root element of a
XmlDocument?

I need to write a GPX document--GPX is a standard XML language for GPS data.
I need to create a root element that looks like this:

<gpx
xmlns="http://www.topografix.com/GPX/1/1" version="1.1"
creator="ExpertGPS 2.4.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
http://www.topografix.com/GPX/gpx_style/0/2
http://www.topografix.com/GPX/gpx_st.../gpx_style.xsd
http://www.topografix.com/GPX/gpx_overlay/0/3
http://www.topografix.com/GPX/gpx_ov...px_overlay.xsd
http://www.topografix.com/GPX/gpx_modified/0/1
http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd">

I can implement the default xmlns attribute by calling
CreateAttribute(prefix, attributeName, uri), using a null prefix and the
topografix.com URI next to the first xmlns attribute. But the element also
declares a second namespace, xmlns:xsi. I don't see how to declare this
namespace, since I used the CreateAttribute() params to declare the default
namespace.

So, how would I declare the xmlns:xsi namespace, so that I can use it in
creating the xsi:schemaLocation attribute that follows it?

Thanks for your help!

David Veeneman
Foresight Systems



Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Adding a second namespace declaration? - 11-10-2007 , 08:08 AM






David Veeneman wrote:

Quote:
So, how would I declare the xmlns:xsi namespace, so that I can use it in
creating the xsi:schemaLocation attribute that follows it?
There is no need at all to create namespace declaration attributes
explictly, it suffices to create elements and attributes in the
namespace they belong to, then the serializer will add the necessary
namespace declaration attributes automatically.

So the following C# code using XmlWriter

const string gpx = "http://www.topografix.com/GPX/1/1",
xsi = "http://www.w3.org/2001/XMLSchema-instance";
using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
writer.WriteStartElement("gpx", gpx);
writer.WriteAttributeString("version", "1.1");
writer.WriteAttributeString("creator", "ExpertGPS 2.4.2");
writer.WriteAttributeString("xsi", "schemaLocation", xsi,
@"http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
http://www.topografix.com/GPX/gpx_style/0/2
http://www.topografix.com/GPX/gpx_st.../gpx_style.xsd
http://www.topografix.com/GPX/gpx_overlay/0/3
http://www.topografix.com/GPX/gpx_ov...px_overlay.xsd
http://www.topografix.com/GPX/gpx_modified/0/1

http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd");
writer.WriteEndElement();
}

writes out the element, its attributes and the namespace declaration
attributes e.g.


<?xml version="1.0" encoding="ibm850"?><gpx version="1.1"
creator="ExpertGPS 2.4
..2" xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd&#xD;&#xA;
http://www.topografix.com/GPX/gpx_style/0/2&#xD;&#xA;
http://www.topografix.com/GPX/gpx_st....xsd&#xD;&#xA;
http://www.topografix.com/GPX/gpx_overlay/0/3&#xD;&#xA;
http://www.topografix.com/GPX/gpx_ov....xsd&#xD;&#xA;
http://www.topografix.com/GPX/gpx_mo.../0/1&#xD;&#xA;
http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1" />


The same holds for using XmlDocument e.g.

const string gpx = "http://www.topografix.com/GPX/1/1",
xsi = "http://www.w3.org/2001/XMLSchema-instance";
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("gpx", gpx);
root.SetAttribute("version", "1.1");
root.SetAttribute("creator", "ExpertGPS 2.4.2");
XmlAttribute schemaLocation = xmlDoc.CreateAttribute("xsi",
"schemaLocation", xsi);
schemaLocation.Value =
@"http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
http://www.topografix.com/GPX/gpx_style/0/2
http://www.topografix.com/GPX/gpx_st.../gpx_style.xsd
http://www.topografix.com/GPX/gpx_overlay/0/3
http://www.topografix.com/GPX/gpx_ov...px_overlay.xsd
http://www.topografix.com/GPX/gpx_modified/0/1
http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd";
root.SetAttributeNode(schemaLocation);
xmlDoc.AppendChild(root);
xmlDoc.Save(Console.Out);

creates

<?xml version="1.0" encoding="ibm850"?>
<gpx version="1.1" creator="ExpertGPS 2.4.2"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd&#xD;&#xA;
http://www.topografix.com/GPX/gpx_style/0/2&#xD;&#xA;
http://www.topografix.com/GPX/gpx_st....xsd&#xD;&#xA;
http://www.topografix.com/GPX/gpx_overlay/0/3&#xD;&#xA;
http://www.topografix.com/GPX/gpx_ov....xsd&#xD;&#xA;
http://www.topografix.com/GPX/gpx_mo.../0/1&#xD;&#xA;
http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1" />


So there is hardly the need to create namespace declaration attributes
explicitly.

If you need to do it nevertheless then make sure you create them in the
proper namespace http://www.w3.org/2000/xmlns/ e.g.

const string gpx = "http://www.topografix.com/GPX/1/1",
xsi = "http://www.w3.org/2001/XMLSchema-instance";
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("gpx", gpx);
XmlAttribute xsiNs = xmlDoc.CreateAttribute("xmlns", "xsi",
"http://www.w3.org/2000/xmlns/");
xsiNs.Value = xsi;
root.SetAttributeNode(xsiNs);

xmlDoc.AppendChild(root);
xmlDoc.Save(Console.Out);

creates

<?xml version="1.0" encoding="ibm850"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1" />


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
David Veeneman
 
Posts: n/a

Default Re: Adding a second namespace declaration? - 11-10-2007 , 12:27 PM



Vielen Dank, Martin!



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.