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
 http://www.topografix.com/GPX/gpx_style/0/2
 http://www.topografix.com/GPX/gpx_st....xsd
 http://www.topografix.com/GPX/gpx_overlay/0/3
 http://www.topografix.com/GPX/gpx_ov....xsd
 http://www.topografix.com/GPX/gpx_mo.../0/1

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
 http://www.topografix.com/GPX/gpx_style/0/2
 http://www.topografix.com/GPX/gpx_st....xsd
 http://www.topografix.com/GPX/gpx_overlay/0/3
 http://www.topografix.com/GPX/gpx_ov....xsd
 http://www.topografix.com/GPX/gpx_mo.../0/1

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/