Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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]

"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

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

--tim

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