如何从字符串更新/替换 XElement 的元素? [英] How can I update/replace an element of an XElement from a string?

查看:19
本文介绍了如何从字符串更新/替换 XElement 的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况.

我有一个 XElement,我们称之为 root,它有后代、后代等等.我使用 LINQ to XML 提取后代,使用 .ToString() 将其加载到备忘录编辑器中并编辑它.现在我想用编辑后的版本更新/替换原始后代元素.

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.

让我提一下,这是一个简单的 XML 文件,没有架构,不使用 DOM 等.我只需要能够编辑和更新/替换元素.

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.

这是我的 XML 的模型:

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>

现在说我要编辑Pink Floyd"元素以更正 Roger Waters 的姓氏.我获取该元素,将其转换为字符串,将其加载到我的编辑器中,进行我想要的更改,然后使用 .Parse() 将其转换回 XElement.

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().

现在,如何更新/替换原始 XML 中的Pink Floyd"节点?

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

推荐答案

您可以使用 XNode.ReplaceWith 方法:

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);

不过,您应该添加一些错误检查.我使用 Single 因为我确定节点存在,但如果有可能它不是你应该使用 SingleOrDefault 并检查 null在使用结果之前.

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.

此外,如果输入无效,您需要将上述代码包装在 try/catch 中并处理可能抛出的任何 XmlException.

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.

这篇关于如何从字符串更新/替换 XElement 的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆