Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中从XML中添加/删除元素#_C#_Asp.net_Xml_C# 4.0_Linq To Xml - Fatal编程技术网

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

C# 在C中从XML中添加/删除元素#,c#,asp.net,xml,c#-4.0,linq-to-xml,C#,Asp.net,Xml,C# 4.0,Linq To Xml,我有以下XML: <states> <state name="Alaska" colour="#6D7B8D"> <Location Name="loc1"> <Address>a1</Address> <DateNTime>a2</DateNTime> </Location> <Location Name="loc2"> <Address>b1</Addre

我有以下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>

a1
a2
b1
b2
c1
c2
d1
d2

我需要从状态添加/删除位置。我该怎么做呢?有人能举例说明使用Linq吗?

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不处理插入或删除

但是,您可以使用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", ...)));

下面是一个示例,说明如何满足您的要求:

  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();
XElement doc=XElement.Parse(“a1 a2 b1 b2 c1 c2 d1 d2”);
文件要素(“国家”)
.Where(s=>s.Attribute(“name”).Value==“阿拉斯加”).Elements(“位置”)
。其中(l=>l.Attribute(“Name”)。Value==“loc1”)
.First()
.Remove();

以下是一个示例,说明如何满足您的要求:

  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();
XElement doc=XElement.Parse(“a1 a2 b1 b2 c1 c2 d1 d2”);
文件要素(“国家”)
.Where(s=>s.Attribute(“name”).Value==“阿拉斯加”).Elements(“位置”)
。其中(l=>l.Attribute(“Name”)。Value==“loc1”)
.First()
.Remove();

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

要删除节点,请搜索要删除的节点,然后删除它们

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

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

要删除节点,请搜索要删除的节点,然后删除它们

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

非常感谢你的回答非常感谢你的回答