簡體   English   中英

如何修改XElement的內容?

[英]How do I modify the contents of an XElement?

有一種簡單的方法來修改XElement的InnerXml嗎? 我們有這個非常簡單的xml

<planets>
    <earth></earth>
    <mercurio></mercurio>
</planets>

我們想要將一些來自另一個來源的xml附加到地球節點中,該字符串就像一個字符串“ <continents><america/><europa/>.....blablabla ”。

我讀了相關的問題,但他們談到檢索XElement的innerxml,我不明白如何“修改”實際的Xelement :(

構建XML

planetsElement.Element("earth").Add(
    new XElement("continents",
        new XElement("america"),
        new XElement("europa")
    )   
);

解析並添加

planetsElement.Element("earth").Add(
   XElement.Parse("<continents><america/><europa/></continents>")
);

使用XElement.ReplaceNodes()設置Element的內容。 所以......

var doc = XDocument.Parse(xmlString);
var earth = doc.Root.Element("earth");

// to replace the nodes use
earth.ReplaceNodes(XElement.Parse("<continents><america/><europa/></continents>"));

// to add the nodes
earth.Add(XElement.Parse("<continents><america/><europa/></continents>"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM