hyperb0le Posted February 23, 2003 Posted February 23, 2003 I have an XML file which reads: <?xml version="1.0"?> <Wallpaper> <Name>Default</Name> </Wallpaper> Now i need to read this file from my c# application and assign the value of <Name> to a string. This is for the preferences file of my app. Thanks in advance. Quote
*Experts* Bucky Posted February 23, 2003 *Experts* Posted February 23, 2003 One way to accomplish this is to load the XML file into an XmlDocument class. Then you can easily access the XML DOM and change elements and attributes, and execuce XPath statements. Here, path is a string containing the file name and path of the XML file: using System.Xml; // This belongs at the top of your code file XmlDocument doc = new XmlDocument(); string myValue; doc.Load(path); myValue = doc.ChildNodes["Name"].InnerText; Sorry if there are any syntax errors in the code, since I've only just started working with C#. This snippet should give you the general idea about how to go about what you want. [edit]Fixed up the code a little[/edit] 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 23, 2003 Leaders Posted February 23, 2003 The GetElementsByTagName method of the XmlDocument class will do using the DOM. If you really need efficiency (i don't at the moment and so haven't invested the time to learn the other approaches), you might want to look at another way though. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmldocumentclassgetelementsbytagnametopic.asp Edit: shucks, looks like I didn't hit submit fast enough:) Quote --tim
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.