我有这个代码来重复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>
所以我的问题是,如何在我的原始元素之后和我的副本之前返回一行?
由于
答案 0 :(得分:0)
解析文档时,请使用:
this._xmldoc = XDocument.Parse(yourXmlString);
而不是:
this._xmldoc = XDocument.Parse(yourXmlString, LoadOptions.PreserveWhitespace);