次の方法で共有


C# で名前空間を持つドキュメントを作成する方法 (LINQ to XML)

この記事では、名前空間を持つ C# でドキュメントを作成する方法について説明します。

例: 既定の名前空間を宣言して初期化する

名前空間内の要素または属性を作成するには、まず、XNamespace オブジェクトを宣言して初期化します。 次に、加算演算子のオーバーロードを使用して、名前空間とローカル名を組み合わせて、文字列として表します。

次の例では、1 つの名前空間を持つドキュメントを作成します。 既定では、LINQ to XML はこのドキュメントを既定の名前空間でシリアル化します。

// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);

この例では、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com">
  <Child>child content</Child>
</Root>

例: 名前空間と属性を持つドキュメントを作成する

次の例では、1 つの名前空間を持つドキュメントを作成します。 また、名前空間プレフィックスを持つ名前空間を宣言する属性も作成します。 プレフィックスを持つ名前空間を宣言する属性を作成するには、属性の名前が名前空間プレフィックスであり、この名前が Xmlns 名前空間にある属性を作成します。 この属性の値は、名前空間の URI です。

// Create an XML tree in a namespace, with a specified prefix
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
    new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);

この例では、次の出力が生成されます。

<aw:Root xmlns:aw="http://www.adventure-works.com">
  <aw:Child>child content</aw:Child>
</aw:Root>

例: プレフィックスを持つ 2 つの名前空間を持つドキュメントを作成する

次の例は、2 つの名前空間を含むドキュメントの作成を示しています。 1 つは既定の名前空間で、もう 1 つはプレフィックスを持つ名前空間です。

ルート要素に名前空間属性を含めることで、http://www.adventure-works.com が既定の名前空間になるように名前空間がシリアル化され、www.fourthcoffee.comfcのプレフィックスでシリアル化されます。 既定の名前空間を宣言する属性を作成するには、xmlnsという名前の属性を名前空間なしで作成します。 属性の値は、既定の名前空間 URI です。

既定の名前空間宣言がスコープ内にある場合は、ローカル名の前に対応する XNamespace オブジェクトを付けることで、子 XElement オブジェクトに適用されます。 一方、既定の名前空間宣言は属性名には直接適用されません。 そのため、既定の名前空間の XAttribute オブジェクトは、ではなく によって、ローカル名の前に対応する XNamespace オブジェクトを付けることで定義されます。

// The http://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
    new XAttribute("xmlns", "http://www.adventure-works.com"),
    new XAttribute(XNamespace.Xmlns + "fc", "www.fourthcoffee.com"),
    new XElement(fc + "Child",
        new XElement(aw + "DifferentChild", "other content")
    ),
    new XElement(aw + "Child2", "c2 content",
        new XAttribute("DefaultNs", "default namespace"),
        new XAttribute(fc + "PrefixedNs", "prefixed namespace")
    ),
    new XElement(fc + "Child3", "c3 content",
        new XAttribute("DefaultNs", "default namespace"),
        new XAttribute(fc + "PrefixedNs", "prefixed namespace")
    )
);
Console.WriteLine(root);

この例では、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
  <fc:Child>
    <DifferentChild>other content</DifferentChild>
  </fc:Child>
  <Child2 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c2 content</Child2>
  <fc:Child3 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c3 content</fc:Child3>

</Root>

例: 2 つの名前空間を持つドキュメントを作成し、両方にプレフィックスを付けます

次の例では、名前空間プレフィックスを持つ 2 つの名前空間を含むドキュメントを作成します。

XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
    new XAttribute(XNamespace.Xmlns + "aw", aw.NamespaceName),
    new XAttribute(XNamespace.Xmlns + "fc", fc.NamespaceName),
    new XElement(fc + "Child",
        new XElement(aw + "DifferentChild", "other content")
    ),
    new XElement(aw + "Child2", "c2 content"),
    new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);

この例では、次の出力が生成されます。

<aw:Root xmlns:aw="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
  <fc:Child>
    <aw:DifferentChild>other content</aw:DifferentChild>
  </fc:Child>
  <aw:Child2>c2 content</aw:Child2>
  <fc:Child3>c3 content</fc:Child3>
</aw:Root>

例: 展開された名前を使用して名前空間を作成する

同じ結果を得るもう 1 つの方法は、XNamespace オブジェクトを宣言して作成する代わりに拡張名を使用することです。

このアプローチは、パフォーマンスに影響します。 LINQ to XML に拡張名を含む文字列を渡すたびに、LINQ to XML は名前を解析し、アトミック化された名前空間を見つけて、アトミック化された名前を見つける必要があります。 このプロセスには CPU 時間がかかります。 パフォーマンスが重要な場合は、XNamespace オブジェクトを明示的に宣言して使用することをお勧めします。

パフォーマンスが重要な問題である場合は、「XName オブジェクトの事前アトミック化」 を参照してください。

// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{http://www.adventure-works.com}Root",
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
    new XElement("{http://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);

この例では、次の出力が生成されます。

<aw:Root xmlns:aw="http://www.adventure-works.com">
  <aw:Child>child content</aw:Child>
</aw:Root>

関連項目