Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xml - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...