Getting and setting attributes of XmlDocument

Posted on 2006-05-03
Last Modified: 2010-04-16
I have a config file that looks like the following:

      <app name=henry>
            <filter value=zxczxc>
                  <level value=TOP/>
      <app name=hank>
            <filter value=qweqwe>
                 <level value=BOTTOM/>

I want to be able to 'get' the attribute value for 'level' in the 'hank' section ONLY and I also
want to be able to set a new value for the same.

How can I accomplish this (code) using the XmlDocument and/or XmlElement classes?
Question by:ipaman
    1 Comment
    LVL 52

    Accepted Solution

    First of all your XML is badly formed. You need quotes around the attribute values. So your XML becomes:

              <app name="henry">
                    <filter value="zxczxc">
                          <level value="TOP" />
              <app name="hank">
                    <filter value="qweqwe">
                         <level value="BOTTOM" />

    Assuming your XML is loaded into an XmlDocument named doc:

                // grab the level node belonging to app node with "henry"
                XmlNode app = doc.SelectSingleNode("/root/app[@name='henry']/filter/level");

                app.Attributes["value"].InnerText;                    // retrieve the value
                app.Attributes["value"].InnerText = "wibble";    // set the value

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now