我有两个XML文件。SourceXml和TargetXml。
我要将一个XML文件的一些元素导入另一个XML文件。
这很好,但是有些元素我只需要改变名称。
目前我正在从头开始构建元素,唯一的区别是XML的名称不同。
让我举个例子。
在我的Source Xml中,我有一个名为
<OldBank>
<SortCode>123456</SortCode>
<AccountNumber>12345678</AccountNumber>
etc....
</OldBank>
在我的目标xml文件中,应该调用名为NewBank的元素,并且它的子元素与原始文件完全相同。
<NewBank>
<SortCode>123456</SortCode>
<AccountNumber>12345678</AccountNumber>
etc....
</NewBank>
我的工作内容如下:
public static void ReplaceNewCustomerDetails(this XDocument xDoc)
{
XElement oldBankElement = GetOldBankElement(xDoc);
var newBakXml= new XElement("NewBank",
new XElement(oldBankElement.ElementOrDefault("SortCode")),
new XElement(oldBankElement.ElementOrDefault("AccountNumber")));
//Build new xml. This is what I do
var newXml = new XElement("MyNewXml");
newXml.Add(newBakXml);
//I wish I could just change the name of the xml rather then building it again
var newXml = new XElement("MyNewXml");
newXml.Add(oldBankElement.Name="NewBank");
任何建议或更优雅的解决方案。
谢谢。