Solved

C# Linq Update XML

Posted on 2016-08-23
5
44 Views
Last Modified: 2016-08-23
Figured out how to update most of the XML elements.  I'm having issues with two.  I want to updated the Attribute and another value as shown below.  I want to update everything that has 123 with ABC.  The structure is more complicated.  I narrowed it down as that is all that is left that I need to update.
<?xml version="1.0" encoding="utf-8"?>
<myXML>
   <person>
       <key ID="123">123</key>
   </person>
<myXML>

Open in new window

XDocument xdoc = XDocument.Load(xml);

var result = (from x in xdoc.Descendants("key")
                      where x.Parent.Name.LocalName == "person"
                      where x.Attribute("ID").Value == sKey
                      select x).FirstOrDefault();

if result != null
{
    result.Element("key").Attribute("ID").SetValue(sKey);  //This gives me object not set error.
    .......
    xdoc.Save(xml);
}

Open in new window

0
Comment
Question by:CipherIS
  • 3
  • 2
5 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Seeming you are already have the XElement with node name Key in result you do not use Element because that would reference a child of Key. Try this.
if result != null
{
    result.Attribute("ID").SetValue(sKey);
    .......
    xdoc.Save(xml);
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
Ah, I was so close.  Thanks.  So, that fixed part of the problem.  Now I have

<key ID="ABC">123</key>

How do I change the value between <key>???</key>

I guess one thing I should add is that when the XML is generated it is generated as

<key ID="123"><othervalue>456</othervalue>123</key>

Not sure if that matters.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
Comment Utility
Sorry about that. This should answer the second question.
if (result != null)
{
    // You set its attribute with this line.
    result.Attribute ( "ID" ).SetValue ( sKey );
    // You change its innerText with this line of code
    result.Value = "ABC";
    xdoc.Save ( "testData.xml" );
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
That seems to be causing me a problem.  It is changing
<key ID="123"><othervalue>456</othervalue>123</key>

Open in new window

To
<key ID="ABC"">ABC</key>

Open in new window

Below is being deleted also.
<othervalue>456</othervalue>
0
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
I modified it not to save the 456.  Thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

15 Experts available now in Live!

Get 1:1 Help Now