Solved

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

Posted on 2011-03-22
3
258 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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