Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update/replace an element of an XElement from a string?

So here's my case.

I have an XElement, let's call it root, which has descendents, which have descendents, etc. I pull a descendent using LINQ to XML, load it into a memo editor using .ToString() and edit it. Now I want to update/replace the original descendent element with the edited version.

Let me mention that this is a simple XML file, with no schema, not using DOM, etc. I only need to be able to edit and update/replace an element.

Here's a mockup of my XML:

<Root>
  <Genre Name="Rock">
    <Artist Name="Pink Floyd">
      <BandMembers>
        <Member>Nick Mason</Member>
        <Member>Syd Barret</Member>
        <Member>David Gilmour</Member>
        <Member>Roger Water</Member>
        <Member>Richard Wright</Member>
      </BandMembers>
      <Category>Favorite band of all time</Category>
    </Artist>
    <Artist Name="Led Zepelin">
      <Category>Love the band</Category>
    </Artist>
  </Genre>
  <Genre Name="Blues">
    <Artist Name="Muddy Waters">
      <Instrument>Harmonica</Instrument>
    </Artist>
    <Artist Name="Howling Wolf">
      <Instrument>Guitar</Instrument>
    </Artist>
  </Genre>
</Root>

Now say I want to edit "Pink Floyd" element to correct Roger Waters' last name. I get that element, convert it to a string, load it into my editor, make the changes that I want, and convert it back to an XElement using .Parse().

Now, how can I update/replace the "Pink Floyd" node in my original XML?

like image 209
Welton v3.61 Avatar asked Apr 28 '11 14:04

Welton v3.61


People also ask

What is XElement C#?

The XElement class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: Create elements. Change the content of the element.


1 Answers

You could use the XNode.ReplaceWith method:

// input would be your edited XML, this is just sample data to illustrate
string input = @"<Artist Name=""Pink Floyd"">
  <BandMembers>
    <Member>Nick Mason</Member>
    <Member>Syd Barret</Member>
    <Member>David Gilmour</Member>
    <Member>Roger Waters</Member>
    <Member>Richard Wright</Member>
  </BandMembers>
  <Category>Favorite band of all time</Category>
</Artist>";

var replacement = XElement.Parse(input);
var pinkFloyd = xml.Elements("Genre")
                   .Where(e => e.Attribute("Name").Value == "Rock")
                   .Elements("Artist")
                   .Single(e => e.Attribute("Name").Value == "Pink Floyd");

pinkFloyd.ReplaceWith(replacement);
Console.WriteLine(xml);

You should add some error checking though. I used Single since I'm sure the node exists, but if there's a chance it isn't you should use SingleOrDefault and check for null before using the result.

Also, if the input is invalid you'll need to wrap the above code in a try/catch and handle any XmlException that might be thrown.

like image 135
Ahmad Mageed Avatar answered Sep 17 '22 17:09

Ahmad Mageed