要素または属性の名前空間をプログラムで変更しなければならない場合があります。 LINQ to XML では、この操作を簡単に行うことができます。 XElement.Name プロパティを設定できます。 XAttribute.Name プロパティは設定できませんが、簡単に属性を System.Collections.Generic.List<T> にコピーし、既存の属性を削除して、目的の新しい名前空間に含まれる新しい属性を追加できます。
詳細については、「名前空間の概要」を参照してください。
例: 2 つの XML ツリーを作成し、名前空間を変更して、ツリーを結合する
次のコードは、名前空間に含まれない 2 つの XML ツリーを作成します。 次に、各ツリーの名前空間を変更して、1 つのツリーに結合します。
XElement tree1 = new XElement("Data",
new XElement("Child", "content",
new XAttribute("MyAttr", "content")
)
);
XElement tree2 = new XElement("Data",
new XElement("Child", "content",
new XAttribute("MyAttr", "content")
)
);
XNamespace aw = "http://www.adventure-works.com";
XNamespace ad = "http://www.adatum.com";
// Change the namespace of every element and attribute in the first tree.
foreach (XElement el in tree1.DescendantsAndSelf())
{
el.Name = aw.GetName(el.Name.LocalName);
List<XAttribute> atList = el.Attributes().ToList();
el.Attributes().Remove();
foreach (XAttribute at in atList)
el.Add(new XAttribute(aw.GetName(at.Name.LocalName), at.Value));
}
// Change the namespace of every element and attribute in the second tree.
foreach (XElement el in tree2.DescendantsAndSelf())
{
el.Name = ad.GetName(el.Name.LocalName);
List<XAttribute> atList = el.Attributes().ToList();
el.Attributes().Remove();
foreach (XAttribute at in atList)
el.Add(new XAttribute(ad.GetName(at.Name.LocalName), at.Value));
}
// Add attribute namespaces so that the tree will be serialized with
// the aw and ad namespace prefixes.
tree1.Add(
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com")
);
tree2.Add(
new XAttribute(XNamespace.Xmlns + "ad", "http://www.adatum.com")
);
// Create a new composite tree.
XElement root = new XElement("Root",
tree1,
tree2
);
Console.WriteLine(root);
Dim tree1 As XElement = _
<Data>
<Child MyAttr="content">content</Child>
</Data>
Dim tree2 As XElement = _
<Data>
<Child MyAttr="content">content</Child>
</Data>
Dim aw As XNamespace = "http://www.adventure-works.com"
Dim ad As XNamespace = "http://www.adatum.com"
' Change the namespace of every element and attribute in the first tree.
For Each el As XElement In tree1.DescendantsAndSelf
el.Name = aw.GetName(el.Name.LocalName)
Dim atList As List(Of XAttribute) = el.Attributes().ToList()
el.Attributes().Remove()
For Each at As XAttribute In atList
el.Add(New XAttribute(aw.GetName(at.Name.LocalName), at.Value))
Next
Next
' Change the namespace of every element and attribute in the second tree.
For Each el As XElement In tree2.DescendantsAndSelf()
el.Name = ad.GetName(el.Name.LocalName)
Dim atList As List(Of XAttribute) = el.Attributes().ToList()
el.Attributes().Remove()
For Each at As XAttribute In atList
el.Add(New XAttribute(ad.GetName(at.Name.LocalName), at.Value))
Next
Next
' Add attribute namespaces so that the tree will be serialized with
' the aw and ad namespace prefixes.
tree1.Add( _
New XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com") _
)
tree2.Add( _
New XAttribute(XNamespace.Xmlns + "ad", "http://www.adatum.com") _
)
' Create a new composite tree.
Dim root As XElement = _
<Root>
<%= tree1 %>
<%= tree2 %>
</Root>
Console.WriteLine(root)
この例を実行すると、次の出力が生成されます。
<Root>
<aw:Data xmlns:aw="http://www.adventure-works.com">
<aw:Child aw:MyAttr="content">content</aw:Child>
</aw:Data>
<ad:Data xmlns:ad="http://www.adatum.com">
<ad:Child ad:MyAttr="content">content</ad:Child>
</ad:Data>
</Root>
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET