删除我的 XML 文件的整个部分 [英] Deleting whole sections of my XML file

查看:22
本文介绍了删除我的 XML 文件的整个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在设计的程序的用户列表,所有用户都存储在一个 XML 文件中,如下所示:

I have a userlist for a program I'm 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>ahost@asd.com</host>
    <email>badrandom@as.com</email>
    <isloggedin>true</isloggedin>
    <permission>10</permission>
  </User>
  <User ID="2">
    <nickname>ohai</nickname>
    <password>asdalkdj9u</password>
    <host>meh@meh.com</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>adb7e51b@webchat.mibbit.com</host>
    <email>alskd@alskd.com</email>
    <isloggedin>true</isloggedin>
    <permission>1</permission>
  </User>
</Users>

现在,根据用户 ID 号,我需要能够删除对该用户的所有引用.

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

那么说,我有ID号3,我如何才能从xml文件中彻底删除用户ID号3的存在?

So say, I have ID number 3, how can I completely delete userid number 3's existence from the xml file?

我正在寻找代码示例,但任何帮助将不胜感激!

I'm looking for code examples, but any help would be greatly appreciated!

推荐答案

使用 XML DOM(.NET 1.x 及更高版本)的一种方法是只加载文件,找到用户编号.3、删除那个节点,把文件保存回来:

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

马克

这篇关于删除我的 XML 文件的整个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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