Solved

C# Linq Update XML

Posted on 2016-08-23
5
52 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
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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

11 Experts available now in Live!

Get 1:1 Help Now