1

xmlns を最初の属性に設定する方法について非常に混乱しました。以下のコードを使用して xml ファイルを生成しました

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

ただし、結果は常に

<?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/">

最初は Iwwant xmlns="http://www.openarchives.org/OAI/2.0/" という意味です。したがって、xElement.FirstAttribute を呼び出すときは、xmlns:xsi ではなく xmlns にする必要があります。

4

1 に答える 1

0

要素の最初の属性として追加して、手動で設定します。

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"),
)
于 2013-06-28T13:52:46.493 に答える