2011-06-25 13 views
14

Intento crear un documento XML de GPX por LINQ a XML.Crear documento XML por LINQ, agregar xmlns, xmlns: xsi a él

Todo funciona muy bien, excepto al agregar atributos xmlns, xmlns: xsi al documento. Al intentarlo de manera diferente, obtengo excepciones diferentes.

Mi código:

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")) 
)); 

La salida también debe contener lo siguiente:

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" 

¿Cómo puedo añadir que por LINQ to XML? Intenté varias maneras, pero no funciona, excepciones durante el tiempo de compilación.

Respuesta

22

Ver How to: Control Namespace Prefixes. Se podría usar un código como éste:

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")) 
)); 

tiene que especificar el espacio de nombres para cada elemento, porque eso es lo que significa el uso de xmlns esta manera.

+0

Estaba buscando exactamente este "xsi: schemaLocation". ¡Gracias! –

10

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

para generar el siguiente nodo raíz y espacios de nombres:

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

Utilice el siguiente código:

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") 
    ) 
); 

Tenga en cuenta - si quieres añadir elementos a la documento, necesita especificar el espacio de nombre predeterminado en el nombre del elemento o obtendrá xmlns = "" agregado a su elemento. Por ejemplo, para agregar un elemento hijo "recuento" al documento anterior, utilice:

xdoc.Add(new XElement(defaultNamespace + "count", 0) 
Cuestiones relacionadas