Jump to content
  • Advertisement
Sign in to follow this  
NickGravelyn

[.net] Adding Children To Existing XML Nodes

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
It'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 :).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!