# [.net] Adding Children To Existing XML Nodes

This topic is 4131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make an updater for csproj files to use some custom settings of mine. Basically I want to drag and drop the csproj file into the app and have it just add a few new parts in. Is there anyway to simultaneously read and write XML using System.Xml? I'm already to getting the filename and creating a backup (in case I mess up), but for instance I need to add a new reference in my app. So I need to get the ItemGroup node that contains the Reference nodes and add a new child. How can I achieve this? Also towards the bottom is the Import tags where I need to add a new one there and remove or comment out another one. Any ideas how to do this? I've tried a few different things and nothing has seemed to work out for me. Thanks for any help.

##### Share on other sites
I asked this same question not so long ago on here and the only reply I got seemed to think you can't just update parts of the xml file. It seems you will need to read in the entire file and write it back with the changes. It might be easier for you to use the File.ReadToEnd and/or textwriter for this instead.

##### Share on other sites
It's quite simple:
XmlDocument document = new XmlDocument();document.Load("myProject.csproj");XmlElement elem = doc.CreateElement("NewNodeName");elem.InnerText = "whatever goes inside";XmlNode k = doc.SelectSingleNode("/path/to/ItemGroup").AppendChild(elem);document.Save("myNewProject.csproj");

##### Share on other sites
Quote:
 Original post by capn_midnightIt's quite simple:*** Source Snippet Removed ***

Wow. That is simple. Thanks. Now I get to take out all that crappy text streaming code I used prior :).

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632959
• Total Posts
3009467
• ### Who's Online (See full list)

There are no registered users currently online

×