C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2004, 11:59 AM   #1
Registered User
 
Join Date: Dec 2003
Posts: 18
XML Navigation

When writing/reading XML in vbScript, everything was easy because I could refer to nodes like an array (ex: main_node[0].sub_node[4]). I am learning C#, and am having problems with XML. All books and resources show me how to write a full file. What if I want to change the value of one element? Let's say I have the following XML file:

<person>
<name>Khelder</name>
<age>21</age>
</person>
<person>
<name>Redlehk</name>
<age>25</age>
</person>

Given the program knows the name "Redlehk", how would you go about reading in his age, and then writing back to the XML file a new age. If anyone can show me how this is done, or point me to a resource, I would greatly appreciate it.

Thanks,

Khelder
Khelder is offline   Reply With Quote
Old 05-26-2004, 02:56 PM   #2
Registered User
 
Strider's Avatar
 
Join Date: Aug 2001
Posts: 149
Here is one way to do it.

Suppose the XML file looks like this:

person.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<person>
		<name>Khelder</name>
		<age>21</age>
	</person>
	<person>
		<name>Redlehk</name>
		<age>25</age>
	</person>
</persons>
Then this will work:
Code:
using System;
using System.Xml;

namespace XmlTest
{
	class XmlTest
	{
		[STAThread]
		static void Main(string[] args)
		{
			// Load the Xml document
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load("person.xml");

			// Create an XmlTextWriter from the document
			XmlTextWriter writer = new XmlTextWriter("person.xml", null);
			writer.Formatting = Formatting.Indented;

			// Loop through each person
			XmlNodeList personList = xmlDoc.GetElementsByTagName("person");
			for(int i = 0; i < personList.Count; i++)
			{
				XmlElement person = (XmlElement) personList[i];

				// Get the person's name
				XmlNodeList personNameList = person.GetElementsByTagName("name");
				XmlElement personName = (XmlElement) personNameList[0];

				// Get the person's age
				XmlNodeList personAgeList = person.GetElementsByTagName("age");
				XmlElement personAge = (XmlElement) personAgeList[0];

				// Compare the data
				if (personName.InnerText == "Redlehk")
					personAge.InnerText = "44";
			}

			// Write the changes
			xmlDoc.WriteContentTo(writer);
			writer.Flush();
			writer.Close();
		}
	}
}
Hope that helps.

David
__________________
One Ring to rule them all, One Ring to find them,
One Ring to bring them all and in the darkness bind them
In the Land of Mordor where the Shadows lie.
Strider is offline   Reply With Quote
Old 05-26-2004, 03:19 PM   #3
Registered User
 
Join Date: Dec 2003
Posts: 18
Thanks!

That is exactly what I was looking for. Thank you. Ill try and implement it into my real project tonight, and I'll be back if I have some more questions.

Thanks again,

Khelder
Khelder is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing Xml deviousdexter C# Programming 7 04-24-2009 06:29 AM
Dissecting an Excel XML spreadsheet desmond5 C++ Programming 1 05-22-2008 04:32 PM
XML encoding issue George2 C# Programming 1 05-16-2008 05:21 AM
XML and data exchange seexml C Programming 0 04-27-2006 03:02 PM
Need help with this xml tree example kzar C Programming 1 11-22-2004 11:23 AM


All times are GMT -6. The time now is 08:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22