Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 13th, 2007, 10:28 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Edit an XML file

Hello. How would I go about editing the data contained in an XML file. Like let's say its called info.xml, and it looks like this:

================================================== ================
<Document>
  <Node1>Information1</Node1>
  <Node2>Information2</Node1>
</Document>
================================================== ================

How could I change the InnerText of <Node1>? Thanks.

If only computers could write programs themselves...
__________________
If only computers could write programs themselves...
 
Old April 14th, 2007, 08:38 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

<sarcasm>
Open the file in notepad, highlight "Information1", type the new data, click File->Save.
</sarcasm> ;)

Now for the answer I assume you really want...
I think this should be pretty close (from memory).
Code:
XmlDocument objDoc = new XmlDocument();
objDoc.Load("info.xml");
XmlNode objNode = objDoc.SelectSingleNode("/Document/Node1");
objNode.Value = "New value";
objDoc.Save("info.xml");
-Peter
 
Old April 15th, 2007, 02:02 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, thank-you for the code! It worked perfectly. The only change required was that "objNode.Value" became "objNode.InnerText". Once again thanks!

If only computers could write programs themselves...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Edit , Translate , Validate XML in one Page moshaik BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 November 24th, 2008 10:37 AM
Edit xml deean XML 19 June 7th, 2008 03:34 AM
Edit .Dat file Mpriya VB How-To 2 May 22nd, 2007 08:43 AM
How to Edit Attribute Value in XML by ASP.Net sibajibasak XML 1 January 20th, 2005 02:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.