Ibexy I
Ibexy I

Reputation: 1143

How do I read this xml File?

I have this xml file

<?xml version="1.0" encoding="utf-8" ?>
<parameters>
    <parameters 
        registerLink="linkValue" 
        TextBox.name="nameValue" 
    />
</parameters>

I want to print off "LinkValue" and "nameValue" by code:

 Console.WriteLine("registerLink: " + registerLink);
 Console.WriteLine("TextBox.name: " + TextBox.name);

Thanks

Upvotes: 2

Views: 162

Answers (2)

Sean Dunford
Sean Dunford

Reputation: 949

Your could use an xml reader like this one

http://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx

Once you have a working sample look here to find out how to open an xml reader from a file stream. File must be located in project directory

http://support.microsoft.com/kb/307548

Once you have that done you can add an open file dialog box to find any file on the computer and even validate the .xml extension and more.

Edit: As you can see in the comments below, Hanks solution is better, faster, and easier. My solution would only be useful if you have huge xml files with tons of data. You may still be interested in the file dialog box as well.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273244

The easiest API is XLinq (System.Xml.Linq)

var doc = XDocument.Load(fileName);
// This should be parameters/parameter, i follow the question with parameters/parameters
var par = doc.Element("parameters").Element("parameters");  
registerLink = par.Attribute("registerLink").Value;  // string

Upvotes: 4

Related Questions