How to add to target objects using XDocument and Linq?
I need to add a large portion of new generated xml to an existing xdoc but only for my nodes which contain a particular value for one of their children. Here's an example:
XDocument originalXML = GetEntityXml(ref exportTile);
XDocument newXML = testr();
XElement xe = new XElement("Subtiles");
var listTileST = from p in originalXML.Descendants("TileST")
where (string)p.Element("TileNumber").Value == "0"
select p;
In my originalXML I am just calling some method to return an XDocument where the tree structure is root->Tiles->TileST where there are a bunch of TileST nodes.Each TileST node has a child called TileNumber and in the example I want the one with a value of 0. newXML contains what I eventually want to add to some node.
开发者_运维百科So now that I retrieved the node I want in listTileST, I don't know where to go. All I want to do is add all the xml in newXML to that retrieved node in listTileST and obviously want it to have an effect the node stored in originalXML.
Have you tried:
foreach( XElement currentElement in listTileSt )
{
currentElement.Add( newXml.Elements( ) );
}
I mostly work with XElement
and not with XDocument
and there you can add one XElement
into another XElement
.
精彩评论