tags:

views:

20

answers:

1

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.

+1  A: 

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.

Viper
Got it, I did nearly the same thing but failed to see that my problem was a forever looping issue due to the fact that the new xml also had a tile number of 0. Thus, I forced it to only look at the TileST nodes from the root node.
Ilya