Vinay
Vinay

Reputation:

I want to get value of an attribute from an XML file

This is the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Books>
    <Book id="1">
        <Author>Mark</Author>
        <Publisher>Sams</Publisher>
    </Book>
</Books>

This is the code to extract the attribute:

 XmlTextReader textReader = new XmlTextReader("D:\\books.xml");
            textReader.MoveToElement();
            string au = textReader.GetAttribute("Auther");
            Uname.Text = au;

Upvotes: 0

Views: 134

Answers (1)

StampedeXV
StampedeXV

Reputation: 2815

Author is not an Attribute, it is an Element of the Element Book.

I think you need some basics of XML first, before you start parsing it.

e.g. http://www.w3schools.com/xmL/

What might help also is looking into XPath. It helps you in selecting Elements. This is helpful especially if you already know the structure of the XML you'd like to parse.

Upvotes: 3

Related Questions