Extracting data from xml and writing.

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
I have this Xml document and I cannot find in my book anywhere about how to simply read a certain nodes information. Can someone help?
Code:
<Appearance>
	<Title>AndrewLog</Title>
	<TitleFont></TitleFont>
	<TitleColor>blue</TitleColor>
	<LogBoxColor></LogBoxColor>
	<LogBoxTextColor></LogBoxTextColor>
</Appearance>

<Functuality>
	<SmallRTBResize>136</SmallRTBResize>
	<LargeRTBResize>300</LargeRTBResize>
	
</Functuality>
All I want to do is extract the settings data from the tags. Please give an example of how to get the TitleColor tags color and put in in Color variable.

I tried this but it does not show anything in the message box:

FileStream F=new FileStream(Org+"\\Settings\\Settings.xml",FileMode.Open);
XmlTextReader reader=new XmlTextReader("Appearance/Title", F);
string g=reader.ReadString();
MessageBox.Show(g);

Also, please a bried explanation of how to write to that same tag.
 
Last edited by a moderator:
The XML Document Object Model (DOM) can be accesed through
the XmlDocument class, found in the System.Xml namespace.
With the XmlDocument class, you can read and write nodes,
attributes, and all kinds of juicy stuff. The class requires a lot of
overhead, however, and for the situation youre describing you
may want to have a look a XML Serialization:

[mshelp]ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.htm[/mshelp]
[mshelp]ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemXmlSerialization.htm[/mshelp]

But in the meantime, you can load the XML file into an
XmlDocument class, read nodes, change them, and write back
out to the file, although with serialization you dont need any of
this code; the .NET framework takes everything and sticks it in
the properties of classes which you can use right away.

Here, fileName is a string containing the path of the XML file. The
value of the TitleColor node is stored in the string titleColorText,
then converted to the Color class titleColor by using
Color.FromName(). Finally, the TitleColor is changed to red and
the file is saved.
C#:
using System.Xml; // Stick this line at the top of your code file

XmlDocument doc = new XmlDocument();

// Load the file
doc.Load(fileName);

// Get the string inside the TitleColor node
string titleColorText = doc.ChildNodes["Appearance"].ChildNodes["TitleColor"].InnerText;

// Convert the color name to a Color class
Color titleColor = Color.FromName(titleColorText);

// Now lets change that color to red
doc.ChildNodes["Appearance"].ChildNodes["TitleColor"].InnerText = "Red";

// Save the file
doc.Save(fileName);

For more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvssamp/html/vbcs_WorkwithXML.asp
 
Ah, Im terribly sorry... You should use GetElementsByTagName(),
which returns an XmlNodeList. Since you will only have one
Appearance element (Im assuming), you can just get the first
element in the list returned and use that.

So chose lines should be as follows:
C#:
// Get the string inside the TitleColor node
string titleColorText = doc.GetElementsByName("Appearance")[0].GetElementsByName("TitleColor")[0].InnerText;

// and...

// Now lets change that color to red
doc.GetElementsByName("Appearance")[0].GetElementsByName("TitleColor")[0].InnerText = "Red";
 
Bucky Im curious, the DOM is obviously a pig because its entirely in memory. On the other hand, you suggest xml serialization which uses mad reflection (yet another incredibly costly operation). I guess I question whether youre really gaining anything using xml serialization over the DOM giving the overhead of reflection to and fro involved in serialization. Are you aware of any metrics or is this just so obvious Im a moron for questioning it?
 
Hmm, Ive never considered the speed and memory required
for serialization... Im afraid I dont have enough knowledge to
answer your question. If they both turn out to be memory and
resource hogs, Id go for serialization because it is much easier to use.
 
That does not work either. It will not let me put the second GetElementsByTagName there. Any you also missed the Tag before Name. Any other ideas?
 
As it turns out, you need to cast the result from GetElementsByTagName
to an XmlElement, because the result is the more generic XmlNode,
which could be an element, an attribute, or anything.

So, here we go:

C#:
using System.Xml; // Stick this line at the top of your code file

XmlDocument doc = new XmlDocument();

// Load the file
doc.Load(fileName);

XmlElement appearanceElement = (XmlElement)doc.GetElementsByTagName("Appearance")[0];
XmlElement titleColorElement = (XmlElement)appearanceElement.GetElementsByTagName("TitleColor")[0];

// Get the string inside the TitleColor node
string titleColorText = titleColorElement.InnerText;

// Convert the color name to a Color class
Color titleColor = Color.FromName(titleColorText);

// Now lets change that color to red
titleColorElement.InnerText = "Red";

// Save the file
doc.Save(fileName);
 
Here is exactly what I have got:

C#:
XmlDocument doc=new XmlDocument();
				doc.Load(Org+"\\Settings\\Settings.xml");
				XmlElement appearanceElement = (XmlElement)doc.GetElementsByTagName("Appearance")[0];
				XmlElement titleColorElement = (XmlElement)appearanceElement.GetElementsByTagName("TitleColor")[0];
				string g = titleColorElement.InnerText;
				MessageBox.Show(g);

Code:
<Appearance>
	<Title>AndrewLog</Title>
	<TitleFont>blue</TitleFont>
	<TitleColor></TitleColor>
	<LogBoxColor></LogBoxColor>
	<LogBoxTextColor></LogBoxTextColor>
</Appearance>
 
Last edited by a moderator:
XmlElement appearanceElement = (XmlElement)doc.GetElementsByTagName("Appearance")[0];

that one.
Isnt there a less cody way to do this using something else like XmlTextReader or XmlReader and Writer.
 
Why dont you investigate them and find out? Theres plenty of documentation on all these classes.
 
My book goes over XmlTextReader but not anything like I am doing. It does not show how to get only certain tag data extracted. The Deitel and Deitel book has several examples of extracting everything!! It is kind of frustrating how there are so many examples of one thing and it is nothing that I will ever (probably) want to use!!!!!!!!!!
 
This works, but there has got to be a better way:

DataSet DS=new DataSet();
DS.ReadXml(Org+"\\Settings\\Settings.xml");
DataRow dr = DS.Tables[0].Rows[0];
string g=(string)dr["Title"];

What if I had another parent tag with child tags the same as other parent tags? The computer would not know which one to pick!
 
The above method is slow. This is faster but I know there must be a better way. Either the registry (which I would like to avoid) or xml.
Does anyone know how to easily read and write from and to xml files for settings?
 
Back
Top