?
Solved

C# Linq Update XML

Posted on 2016-08-23
5
Medium Priority
?
89 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
[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
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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