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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…