aewarnick Posted February 25, 2003 Posted February 25, 2003 (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 February 25, 2003 by aewarnick Quote C#
*Experts* Bucky Posted February 25, 2003 *Experts* Posted February 25, 2003 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 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 ChildNodes want an int in the brackets not a string value. It will not compile. Quote C#
*Experts* Bucky Posted February 25, 2003 *Experts* Posted February 25, 2003 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"; Quote "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 quwiltw Posted February 25, 2003 Leaders Posted February 25, 2003 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? Quote --tim
*Experts* Bucky Posted February 25, 2003 *Experts* Posted February 25, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 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? Quote C#
*Experts* Bucky Posted February 25, 2003 *Experts* Posted February 25, 2003 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); Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 It compiles but produces an error. Xml exception. System error. Quote C#
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 (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 February 25, 2003 by divil Quote C#
*Experts* Bucky Posted February 25, 2003 *Experts* Posted February 25, 2003 I ran the code on my computer just fine... which line is the error occuring on? Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 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. Quote C#
*Gurus* divil Posted February 25, 2003 *Gurus* Posted February 25, 2003 Why don't you investigate them and find out? There's plenty of documentation on all these classes. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 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!!!!!!!!!! Quote C#
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 I think I found the page I was looking for: http://www.c-sharpcorner.com/Code/2002/May/SaveNRestoreFormSettings.asp Quote C#
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 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! Quote C#
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 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? Quote C#
aewarnick Posted February 25, 2003 Author Posted February 25, 2003 I guess I will use the registry. Xml seems to Cody and slow. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.