Unable to read xml attribute... why?

Posted on 2007-08-12
Last Modified: 2010-04-15
I have the following C# code snippet:
------------------------    start    -----------------------
XmlTextReader tr = new XmlTextReader(xmlFilename);
            if (File.Exists(xmlFilename))
                tr.MoveToElement();  // Move to first element
                googleFileID = tr.GetAttribute("RegFileID");  //<-------------------------- not working
-----------------------    end of code    ----------------------

and this is the xml file I am trying to read with the above code:

-------------------------     start    --------------------------
<?xml version="1.0"?>
<GoogleSettings UserID="rolfmarsh" RegFileID="abc" BookStoreName="prager" VISA="Y" MasterCard="Y" AMEX="Y" Discover="N" PayPalDoesAll="Y" GoogleCheckout="N" Location="" MediaMail="N" PriorityMail="N" ExpressMail="N" Intl="N" IntlAir="N" UPSGround="N" UPSAir="N" />
--------------------------    end    ----------------------------

Can someone please tell me why the code won't read the attribute?
Question by:rmmarsh
    LVL 22

    Accepted Solution

    To get to the beginning, you need to:

    Then read the attribute.


    Author Comment

    You're the man!  Thank you so much!
    LVL 7

    Expert Comment

    Firstly, I would imagine you would want to move XmlTextReader tr = new XmlTextReader(xmlFilename); to inside the if statement to avoid the overhead of creating the reader if the file doesn't exist. However, I can imagine cases where putting it outside is legitimate, so apologies if yours is one of those cases.

    As JimBrandley suggested, you need to use MoveToContent, so the code would look like this:

    XmlTextReader tr = new XmlTextReader(xmlFilename);
    if (File.Exists(xmlFilename))
        googleFileID = tr.GetAttribute("RegFileID");
    LVL 22

    Expert Comment

    My pleasure. Good luck!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now