Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-22
3
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

722 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