Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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?

<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.

Edited by aewarnick
C#
  • *Experts*
Posted

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 you're 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 don't 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.

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 let's 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

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

Ah, I'm terribly sorry... You should use GetElementsByTagName(),

which returns an XmlNodeList. Since you will only have one

Appearance element (I'm assuming), you can just get the first

element in the list returned and use that.

 

So chose lines should be as follows:

// Get the string inside the TitleColor node
string titleColorText = doc.GetElementsByName("Appearance")[0].GetElementsByName("TitleColor")[0].InnerText;

// and...

// Now let's change that color to red
doc.GetElementsByName("Appearance")[0].GetElementsByName("TitleColor")[0].InnerText = "Red";

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • Leaders
Posted
Bucky I'm curious, the DOM is obviously a pig because it's 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 you're 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 I'm a moron for questioning it?
--tim
  • *Experts*
Posted

Hmm, I've never considered the speed and memory required

for serialization... I'm afraid I don't have enough knowledge to

answer your question. If they both turn out to be memory and

resource hogs, I'd go for serialization because it is much easier to use.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

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:

 

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 let's change that color to red
titleColorElement.InnerText = "Red";

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

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted (edited)

Here is exactly what I have got:

 

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);

 

<Appearance>
<Title>AndrewLog</Title>
<TitleFont>blue</TitleFont>
<TitleColor></TitleColor>
<LogBoxColor></LogBoxColor>
<LogBoxTextColor></LogBoxTextColor>
</Appearance>

Edited by divil
C#
  • *Experts*
Posted

I ran the code on my computer just fine... which line is the error

occuring on?

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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

 

that one.

Isn't there a less cody' way to do this using something else like XmlTextReader or XmlReader and Writer.

C#
Posted
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!!!!!!!!!!
C#
Posted

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!

C#
Posted

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?

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...