简体   繁体   中英

Add/Remove elements from XML in C#

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?

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", ...)));

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM