0

hi i have xml files and i need to append them like this:

First File:

<Tags>
    <Tag name ="1">
        //more xml tags
    </Tag>

    <Tag name = "2">
        //some more xml tags

    </Tag>

    .....

    //add second file here

</Tags>

Second File:

 <Tag name ="3">
     //more xml tags
 </Tag>
2
  • Please don't just ask us to solve the problem for you. Show us how you tried to solve the problem yourself, then show us exactly what the result was, and tell us why you feel it didn't work. See "What Have You Tried?" for an excellent article that you really need to read. Commented Dec 16, 2013 at 11:04
  • Ok, I will know from now on. Commented Dec 16, 2013 at 11:09

2 Answers 2

1

Use ImportNode method:

var d1 = new XmlDocument();
d1.LoadXml("<Tags><Tag name =\"1\"></Tag></Tags>");

var d2 = new XmlDocument();
d2.LoadXml("<Tags><Tag name =\"2\"></Tag></Tags>");

var newNode = d1.ImportNode(d2.SelectSingleNode("/Tags/Tag"), true);
d1.DocumentElement.AppendChild(newNode);

Console.WriteLine(d1.OuterXml);

Here is the fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

That's not adding anything.
@Georgi It was just a use case :) See y updated answer.
1

I suggest you to use LINQ to XML:

var firstDoc = XDocument.Load("file1.xml"); // load 1st file
var tagElement = XElement.Load("file2.xml"); // load <Tag> element from 2nd file
firstDoc.Root.Add(tagElement); // add <Tag> element to 1st file <Tags> element
firstDoc.Save("file1.xml"); // save 1st file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.