[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting and setting attributes of XmlDocument

Posted on 2006-05-03
1
Medium Priority
?
333 Views
Last Modified: 2010-04-16
I have a config file that looks like the following:

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

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?
0
Comment
Question by:ipaman
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 16599108
First of all your XML is badly formed. You need quotes around the attribute values. So your XML becomes:

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

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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

831 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