0

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>

だから私の質問は、元の要素の後、コピーの前に行を返す方法は?

ありがとう

4

1 に答える 1

0

ドキュメントを解析するときは、次を使用します。

this._xmldoc = XDocument.Parse(yourXmlString);

それ以外の:

this._xmldoc = XDocument.Parse(yourXmlString, LoadOptions.PreserveWhitespace);
于 2013-08-05T09:55:12.380 に答える