tags:

views:

96

answers:

3

Alright, so here is my situation. i have a userlist for a program im designing, and all the users are stored to an XML file, like so:

<?xml version="1.0"?>
<Users>
  <User ID="1">
    <nickname>Tom</nickname>
    <password>Sams</password>
    <host>[email protected]</host>
    <email>[email protected]</email>
    <isloggedin>true</isloggedin>
    <permission>10</permission>
  </User>
  <User ID="2">
    <nickname>ohai</nickname>
    <password>asdalkdj9u</password>
    <host>[email protected]</host>
    <email>my@email</email>
    <isloggedin>false</isloggedin>
    <permission>1</permission>
  </User>
  <User ID="3">
    <nickname>ohai</nickname>
    <password>sercret</password>
    <host>my@host</host>
    <email>my@email</email>
    <isloggedin>false</isloggedin>
    <permission>1</permission>
  </User>
  <User ID="4">
    <nickname>mib_hr6qhr</nickname>
    <password>YXNsa2RhZGxrYXNk</password>
    <host>[email protected]</host>
    <email>[email protected]</email>
    <isloggedin>true</isloggedin>
    <permission>1</permission>
  </User>
</Users>

now, based on the users ID number, i need to be able to delete all reference to that user.

So say, i have ID number 3. how can i completely delete userid number 3's existance from the xml file?

im looking for code examples.. any help would be greatly appriciated!

+5  A: 

One approach using the XML DOM (.NET 1.x and up) would be to just load the file, find user no. 3, and remove that node, and save the file back:

XmlDocument doc = new XmlDocument();
doc.Load("yourXmlFile.xml");

XmlNode userNo3 = doc.SelectSingleNode("//Users/User[@ID='3']");

if(userNo3 != null)
{
   userNo3.ParentNode.RemoveChild(userNo3);
}

doc.Save("YourNewXmlFile.xml");

Marc

marc_s
`doc.RemoveChild(userNo3)` - wouldn't this only work if `userNo3` is a top-level node? Don't you actually need `userNo3.Parent.RemoveChild(userNo3)` ?
AakashM
@AakashM - thanks for pointing that out - you're 100% right, but your syntax is not quite right - I updated my post accordingly. Thanks!
marc_s
+4  A: 

Assuming you have the XML loaded in an XDocument:

using System.Linq;
using System.Xml.Linq;

void Delete()
{
    XDocument document = LoadXML();
    document.Elements("Users")
        .Single(e => e.Attribute("ID").Value == "3")
        .Remove();
}

Of course, this assumes that the user you request will always be present in the XML. To be safe, you should use SingelOrDefault() and check for a null value before deleting.

Steve Guidi
Linq-to-XML - works great, but only on .NET 3.5 and up
marc_s
A: 

The problem with linq and DOM approach is that there is a round-trip (parsing/serialization) which can be efficient, vtd-xml has something called incremental update, here is an article that explains it... http://www.codeproject.com/KB/XML/xml_processing_future.aspx

VTDGen vg = new VTDGen();
if (vg.parseFile("your.xml", false)){
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("/users/user[@ID='3']");
    XMLModifier xm = new XMLModifier(vn);
    if (ap.evalXPath()!=-1){
       xm.remove();
       xm.output("new.xml");
    }
}
vtd-xml-author