Create XML doc by LINQ, add xmlns,xmlns:xsi to it
Asked Answered
A

2

17

I try to create an GPX XML document by LINQ to XML.

Everything works great, except adding xmlns, xmlns:xsi attributes to the doc. By trying it different way I get different exceptions.

My code:

XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "no"),
new XElement("gpx",
new XAttribute("creator", "XML tester"),
new XAttribute("version","1.1"),
new XElement("wpt",
new XAttribute("lat","7.0"),
new XAttribute("lon","19.0"),
new XElement("name","test"),
new XElement("sym","Car"))
));

The output should also contain this:

xmlns="http://www.topografix.com/GPX/1/1" 
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"

How can I add it by Linq to XML? I tried several ways but it does not work, exceptions during compile time.

Anticlerical answered 25/6, 2011 at 21:3 Comment(0)
S
27

See How to: Control Namespace Prefixes. You could use code like this:

XNamespace ns = "http://www.topografix.com/GPX/1/1";
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
XDocument xDoc = new XDocument(
    new XDeclaration("1.0", "UTF-8", "no"),
    new XElement(ns + "gpx",
        new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
        new XAttribute(xsiNs + "schemaLocation",
            "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"),
        new XAttribute("creator", "XML tester"),
        new XAttribute("version","1.1"),
        new XElement(ns + "wpt",
            new XAttribute("lat","7.0"),
            new XAttribute("lon","19.0"),
            new XElement(ns + "name","test"),
            new XElement(ns + "sym","Car"))
));

You have to specify the namespace for each element, because that's what using xmlns this way means.

Spenserian answered 25/6, 2011 at 21:20 Comment(1)
I was looking exactly for this "xsi:schemaLocation". Thank you!Hadhramaut
T
10

From http://www.falconwebtech.com/post/2010/06/03/Adding-schemaLocation-attribute-to-XElement-in-LINQ-to-XML.aspx:

To generate the following root node and namespaces:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.foo.bar someSchema.xsd" 
xmlns="http://www.foo.bar" >
</root>

Use the following code:

XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace defaultNamespace = XNamespace.Get("http://www.foo.bar");
XElement doc = new XElement(
    new XElement(defaultNamespace + "root",
    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
    new XAttribute(xsi + "schemaLocation", "http://www.foo.bar someSchema.xsd")
    )
);

Be aware - if you want to add elements to the document, you need to specify the defaultNamespace in the element name or you will get xmlns="" added to your element. For example, to add a child element "count" to the above document, use:

xdoc.Add(new XElement(defaultNamespace + "count", 0)
Thermos answered 25/6, 2011 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.