あなたはツリーウォークをしなければならないと私は信じています。
あなたが場所ですべての属性を保持したい場合は、XElement.ReplaceAttributes
を使用する必要があるだろう。それ以外の場合は、古いものを削除して新しいものを追加することができます。 (あなたはXAttribute
の名前を変更することはできません。)
サンプルコードを:
using System.Linq;
using System.Xml.Linq;
class Test
{
static void Main()
{
var xdoc = new XDocument(
new XElement("root",
new XElement("child1",
new XAttribute("NotArray", "foo"),
new XAttribute("Array", "bar")
),
new XElement("child2",
new XAttribute("Array", 0),
new XAttribute("Y", 1)
)
)
);
Console.WriteLine("Before:");
Console.WriteLine(xdoc);
Console.WriteLine();
XNamespace ns = "@Json";
var attributesToReplace = xdoc
.Descendants()
.Attributes("Array")
.ToList();
foreach (var attribute in attributesToReplace)
{
var element = attribute.Parent;
attribute.Remove();
element.Add(new XAttribute(ns + "Array", attribute.Value));
}
Console.WriteLine("After:");
Console.WriteLine(xdoc);
}
}
出力:
Before:
<root>
<child1 NotArray="foo" Array="bar" />
<child2 Array="0" Y="1" />
</root>
After:
<root>
<child1 NotArray="foo" p2:Array="bar" xmlns:p2="@Json" />
<child2 Y="1" p2:Array="0" xmlns:p2="@Json" />
</root>
[MCVE]あなたが提供したい場合には役立つだろう - ということに注意してくださいすべてのJSONとデータベースの部分は無関係ですが、それらをバックグラウンドの動機付けとして含めることは妥当かもしれません。 –
文字列に変換する必要はありませんが、ツリーウォークは... –