Solved

C# Linq Update XML

Posted on 2016-08-23
5
67 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 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41767108
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
ID: 41767142
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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 41767186
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
ID: 41767248
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
ID: 41767387
I modified it not to save the 456.  Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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