在 C# 中从 XML 添加/删除元素

发布于 2024-12-29 04:33:52 字数 734 浏览 1 评论 0原文

我有这个 XML:

<states>
<state name="Alaska" colour="#6D7B8D">
<Location Name="loc1">
  <Address>a1</Address>
  <DateNTime>a2</DateNTime>
</Location>
<Location Name="loc2">
  <Address>b1</Address>
  <DateNTime>b2</DateNTime>
</Location>
</state>
<state name="Wyoming" colour="#6D7B8D">
<Location Name="loc3">
  <Address>c1</Address>
  <DateNTime>c2</DateNTime>
</Location>
<Location Name="loc4">
  <Address>d1</Address>
  <DateNTime>d2</DateNTime>
</Location>
</state>
</states>

我需要从州添加/删除位置。我该怎么做呢?谁能用一个例子解释如何使用 Linq?

I have this XML:

<states>
<state name="Alaska" colour="#6D7B8D">
<Location Name="loc1">
  <Address>a1</Address>
  <DateNTime>a2</DateNTime>
</Location>
<Location Name="loc2">
  <Address>b1</Address>
  <DateNTime>b2</DateNTime>
</Location>
</state>
<state name="Wyoming" colour="#6D7B8D">
<Location Name="loc3">
  <Address>c1</Address>
  <DateNTime>c2</DateNTime>
</Location>
<Location Name="loc4">
  <Address>d1</Address>
  <DateNTime>d2</DateNTime>
</Location>
</state>
</states>

I need to Add/Delete locations from state. How can I go about doing this? Can anyone explain using Linq with an example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2025-01-05 04:33:52

Linq 不处理插入或删除。

但您可以使用 XLinq 库来实现这两者。

var doc = XDocument.Load(fileName);  // or .Parse(xmlText);

var alaska = doc.Root.Elements("state")
       .Where(e => e.Attribute("name").Value == "Alaska").First();

alaska.Add(new XElement("Location", new XAttribute("Name", "someName"), 
       new XElement("Address", ...)));

Linq doesn't handle inserts or deletes.

But you can use the XLinq library to both.

var doc = XDocument.Load(fileName);  // or .Parse(xmlText);

var alaska = doc.Root.Elements("state")
       .Where(e => e.Attribute("name").Value == "Alaska").First();

alaska.Add(new XElement("Location", new XAttribute("Name", "someName"), 
       new XElement("Address", ...)));
极度宠爱 2025-01-05 04:33:52

要添加节点,请搜索要添加的父元素,创建要添加的元素,然后添加它。

要删除节点,请搜索要删除的节点,然后将其删除。

// load the xml
var doc = XDocument.Load(@"C:\path\to\file.xml");

// add a new location to "Alaska"
var parent = doc.Descendants("state")
    .Where(e => (string)e.Attribute("name") == "Alaska")
    .SingleOrDefault();

if (parent != null)
{
    // create a new location node
    var location =
        new XElement("Location",
            new XAttribute("Name", "loc5"),
            new XElement("Address", "e1"),
            new XElement("DateNTime", "e2")
        );

    // add it
    parent.Add(location);
}

// remove a location from "Wyoming"
var wyoming = doc.Descendants("state")
    .Where(e => (string)e.Attribute("name") == "Wyoming")
    .SingleOrDefault();

if (wyoming != null)
{
    // remove "loc4"
    wyoming.Elements(e => (string)e.Attribute("Name") == "loc4")
           .Remove();
}

// save back to the file
doc.Save(pathToFile);

To add nodes, search for the parent element you wish to add to, create the element you want to add then add it.

To remove nodes, search for the nodes you wish to remove then remove them.

// load the xml
var doc = XDocument.Load(@"C:\path\to\file.xml");

// add a new location to "Alaska"
var parent = doc.Descendants("state")
    .Where(e => (string)e.Attribute("name") == "Alaska")
    .SingleOrDefault();

if (parent != null)
{
    // create a new location node
    var location =
        new XElement("Location",
            new XAttribute("Name", "loc5"),
            new XElement("Address", "e1"),
            new XElement("DateNTime", "e2")
        );

    // add it
    parent.Add(location);
}

// remove a location from "Wyoming"
var wyoming = doc.Descendants("state")
    .Where(e => (string)e.Attribute("name") == "Wyoming")
    .SingleOrDefault();

if (wyoming != null)
{
    // remove "loc4"
    wyoming.Elements(e => (string)e.Attribute("Name") == "loc4")
           .Remove();
}

// save back to the file
doc.Save(pathToFile);
蓝天白云 2025-01-05 04:33:52

以下是如何执行您所要求的操作的示例:

  XElement doc = XElement.Parse("<states><state name=\"Alaska\" colour=\"#6D7B8D\"><Location Name=\"loc1\">  <Address>a1</Address>  <DateNTime>a2</DateNTime></Location><Location Name=\"loc2\">  <Address>b1</Address>  <DateNTime>b2</DateNTime></Location></state><state name=\"Wyoming\" colour=\"#6D7B8D\"><Location Name=\"loc3\">  <Address>c1</Address>  <DateNTime>c2</DateNTime></Location><Location Name=\"loc4\">  <Address>d1</Address>  <DateNTime>d2</DateNTime></Location></state></states>");

   doc.Elements("state")
       .Where(s => s.Attribute("name").Value == "Alaska").Elements("Location")
            .Where(l => l.Attribute("Name").Value == "loc1")
            .First()
            .Remove();

Here is an example of how you can do what you are asking:

  XElement doc = XElement.Parse("<states><state name=\"Alaska\" colour=\"#6D7B8D\"><Location Name=\"loc1\">  <Address>a1</Address>  <DateNTime>a2</DateNTime></Location><Location Name=\"loc2\">  <Address>b1</Address>  <DateNTime>b2</DateNTime></Location></state><state name=\"Wyoming\" colour=\"#6D7B8D\"><Location Name=\"loc3\">  <Address>c1</Address>  <DateNTime>c2</DateNTime></Location><Location Name=\"loc4\">  <Address>d1</Address>  <DateNTime>d2</DateNTime></Location></state></states>");

   doc.Elements("state")
       .Where(s => s.Attribute("name").Value == "Alaska").Elements("Location")
            .Where(l => l.Attribute("Name").Value == "loc1")
            .First()
            .Remove();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文