1

I was very confused about the how to set xmlns to be the first attribute, I used below code to generate a xml file

XNamespace ns = "http://www.openarchives.org/OAI/2.0/";
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.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"),
));

howevery, the result is always

<?xml version="1.0" encoding="UTF-8"?>
<gpx 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://
www.openarchives.org/OAI/2.0/OAI-PMH.xsd"
xmlns="http://www.openarchives.org/OAI/2.0/">

I mean Iwwant xmlns="http://www.openarchives.org/OAI/2.0/" at the first. so when I call xElement.FirstAttribute, it should be xmlns rather than xmlns:xsi, any idea?

pnuts
  • 58,317
  • 11
  • 87
  • 139
danmiao
  • 747
  • 2
  • 8
  • 17

1 Answers1

0

Set it manually by adding it as the first attribute in the element:

new XElement(ns + "gpx",
    new XAttribute("xmlns", ns.NamespaceName), // add it here
    new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
    new XAttribute(xsiNs + "schemaLocation",
        "http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"),
)
g t
  • 7,287
  • 7
  • 50
  • 85