tags:

views:

39

answers:

2

is there a simple way to modify the InnerXml of a XElement? supose we have this extremely simple xml

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

and we want to append some xml that come from another source that comes like a string "<continents><america/><europa/>.....blablabla" into the earth node.

I read related questions but they talk about retrieving the innerxml of a XElement and i don't understand how "modify" the actual Xelement :(

+1  A: 

Use XElement.ReplaceNodes() to set the content of your Element. So ...

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>"));
Obalix
+1  A: 

Build the XML

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

Parse and Add

planetsElement.Element("earth").Add(
   XElement.Parse("<continents><america/><europa/></continents>")
);
ChaosPandion
The source is a string, supposed we have a string with thousand of nodes, this aproach isn't useful. The XmlElement have the innerXml property and only a assignation is need it. My cuestion target if there is a simple way like that.
voodoomsr
ouyea "Parse and Add" is what I need, thanks!!
voodoomsr
@voodoomsr - Don't forget to accept an answer.
ChaosPandion
ou ok Chaos i didn't know that
voodoomsr