我有此代码用于在 XDocument 对象中复制 xml 元素。
//get a copy of element
XElement element = new XElement(this._xmldoc.Descendants(name).LastOrDefault());
//add element
this._xmldoc.Descendants(name).LastOrDefault().AddAfterSelf(element);
[编辑]
这是我的“保存”方法
/// <summary>
/// Method used to save document
/// </summary>
/// <param name="filePath">file path</param>
public bool Save(string filePath)
{
//value to return
bool returnValue = true;
//clean empty elements
returnValue &= this.CleanEmptyElements();
//normalize xml text
returnValue &= this.NormalizeDocument();
try
{
//save document
this._xmldoc.Save(filePath);
}
catch (InvalidOperationException)
{
returnValue = false;
}
//if save operation has failed
if (!returnValue)
//delete file
File.Delete(filePath);
//end of method
return returnValue;
}
[/编辑]
我的代码运行良好,但复制后,我有一个小问题,我有这个 XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<Invoice>
<Party>
<Country>FRANCE</Country>
</Party><Party>
<Country>SPAIN</Country>
</Party>
</Invoice>
我想要这个 XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<Invoice>
<Party>
<Country>FRANCE</Country>
</Party>
<Party>
<Country>SPAIN</Country>
</Party>
</Invoice>
所以我的问题是,如何在原始元素之后和副本之前返回一行?
谢谢