Solved

How can I modify an xml value based on the attribute value.

Posted on 2011-03-22
3
257 Views
Last Modified: 2012-05-11
It does not look like its as easy as selectsinglenode. I could be wrong though.
Example....

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("myfile.xml");            

From......                  
                        
<root>
    <properties>
        <property name="index1">myvalue1</property>
        <property name="index2">myvalue2</property>
        <property name="index3">myvalue3</property>
      </properties>
</root>


To.... (notice index2 value has changed)

<root>
    <properties>
        <property name="index1">myvalue1</property>
        <property name="index2">myvalue2-MODIFIED</property>
        <property name="index3">myvalue3</property>
      </properties>
</root>
0
Comment
Question by:silentthread2k
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35189964
You should be able to do something like:
XmlNode node = xmldoc.SelectSingleNode("/root/properties/property[@name='index2']");
if (node != null)
{
    node.InnerText = node.InnerText + "-MODIFIED";
}

// save document, etc

Open in new window

0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35190249
Hi,
You can also do it with InnerXml property also.
node.InnerXml= node.InnerXml+ "-MODIFIED";
0
 

Author Closing Comment

by:silentthread2k
ID: 35199675
:-)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 107
System.Security 2 27
Sent email using Shared Mailbox from outlook in C# 5 24
MailAddress in vb 4 15
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

773 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