XObjects を使用して、名前空間属性と属性値を持つ "dcterms:type" 要素をプログラムで作成したいと考えています。ターゲット xml は次のとおりです。
<metadata
xmlns="http://example.org/myapp/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/myapp/ http://example.org/myapp/schema.xsd"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">
...
<dcterms:type xsi:type="dcterms:URI">test</dcterms:type>
...
</metadata>
私が試してみました:
XNamespace dcterms= XNamespace.Get("http://purl.org/dc/terms/");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");
XAttribute xsiAttribute = new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName);
XAttribute dcAttribute = new XAttribute(XNamespace.Xmlns + "dc", dc.NamespaceName);
XAttribute dcmitypeAttribute = new XAttribute(XNamespace.Xmlns + "dcterms", dcterms.NamespaceName);
...
XElement typeElement = new XElement(dc + "type", "test");
XAttribute typeAttribute = new XAttribute(xsi + "type", dcterms + "URI");
typeElement.Add(typeAttribute);
しかし、これは以下を生成します:
<dc:type xsi:type="{http://purl.org/dc/terms/}Text">test</dc:type>
...これは間違っており、もちろん検証されません。また、値をハードコーディングしてみました:
XAttribute typeAttribute = new XAttribute(xsi + "type", @"dcterms:URI");
これにより、正しい xml が生成されますが、「これは無効な xsi:type ' http://purl.org/dc/terms/:Text ' です」が返されます。XDocument.Validate() を介して検証された場合。
アイデア?