index > Visual C# General > Add Node existing XML file

Add Node existing XML file

- <
cablehead
Hi. I think is what you looking for


//file name
string filename = @"d:\temp\XMLFile2.xml";

//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();

//load from file
doc.Load(filename);

//create node and add value
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
node.InnerText = "this is new node";

//add to elements collection
doc.DocumentElement.AppendChild(node);

//save back
doc.Save(filename);


Hope this helps



Galin Iliev [MCSD.NET, MCPD], http://www.galcho.com
Galin Iliev

Returns <

cablehead

sorry but I am not able to understand your question... where is the favorites section?

What result do you want to achieve?




Galin Iliev [MCSD.NET, MCPD], http://www.galcho.com
Galin Iliev

This XML contains a Country and Favorites "section". There will potentially be 50 or so "sections.

cablehead
here is modified code that append following to the end of file

//file name
string filename = @"d:\temp\XMLFile2.xml";

//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();

//load from file
doc.Load(filename);

//create node and add value
XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
//node.InnerText = "this is new node";

//create title node
XmlNode nodeTitle = doc.CreateElement("Title");
//add value for it
nodeTitle.InnerText = "This title is created by code";

//create Url node
XmlNode nodeUrl = doc.CreateElement("Url");
nodeUrl.InnerText = @"http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&SiteID=1";

//add to parent node
node.AppendChild(nodeTitle);
node.AppendChild(nodeUrl);

//add to elements collection
doc.DocumentElement.AppendChild(node);

//save back
doc.Save(filename);


and to the end is appended

<Genre_Genre_Country>
<Title>This title is created by code</Title> <Url>http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&amp;SiteID=1</Url>
</Genre_Genre_Country>


Take a look at the code. You can change nodes at whatever you want as they are presented as string

Hope this helps



Galin Iliev [MCSD.NET, MCPD], http://www.galcho.com
Galin Iliev
Thanks again Galin....my hair is now growing back.


I need a job!
cablehead
reply 7

You can use google to search for other answers

 

More Articles

• Close Right Click On Flash
• How do I embed an SQL Server Express database with a C# applicati...
• Loading .pvk file
• Decode URL string in C# (not ASP.Net)
• Tooltip Text
• wanting to read/write image files - .bmp, gif, jpeg
• How to find IP Address
• Sound In Form...
• Private members ... global variable??
• DllImport call using a struct as param throwing exception
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Planning for MCP 316
• hidden text in richtextbox
• Is XML Config files for project specific
• Make program compatible with older .net
• Splash Form ProgressBar Update
• bytes in a byte[]
• Connect to database on server
• Generating .exe file as project's Output
• BackgroundWorker Intelligent Drag drop C
• Why is this variable not visible?
• Developing Windows Screen saver with Fla
• NUnit forms test, Tricky exception:Resou
• Merging MSSQL Databases in C#
• Config file for a windows form app
• redirect IE frames

Hot Articles

• .Net 2.0 Winform takes time to load cont
• [C#]How can I download a ".gif &quo
• Programmatically controlling Visio using
• ORA-12154 error when connection to Oracl
• Visual inheritance and layoutPanel probl
• Thread problem
• How can I post HTML contents with HttpWe
• devenv command line and strong names
• How to pass reference parameter to unman
• Link a file type to my application?
• How to catch exception from third party
• autoeventWireup bug?
• Is this possible??? Word document to SQL
• DefaultValue Attribute for property of t
• accessing the parent object in my heirar

Recommend Articles

• How to sign EXE using signtool with Veri
• convert image to icon
• How to convert Image files to stream...
• display xml file
• How can I disable selecting text from my
• How to insert actual datetime with secon
• Custom Button in BindingNavigator panel.
• problem with Smart Navigation in ASP.NET
• read/write appsettings in beta2
• How can I create a favorites list for a
• How to programmatically set FocusedItem
• Problems with Nullable DateTime Type.
• C#.net 2005 to Excel 2003 - Insert funct
• set-up files for C#
• Why doesn't this work?!