类文件:

 class OperatorXML
{
/// <summary>
/// 确定资源文件路径,Resource为自己创建的目录
/// </summary>
private string filePath = @"..\..\Resources\XMLFile.xml";
/// <summary>
///
/// </summary>
private XDocument xDoc ; /// <summary>
/// 当窗体加载时, 遍历所有节点
/// </summary>
public List<Desktop> LoadNode()
{
List<Desktop> desktop = new List<Desktop>();
try
{
this.xDoc = XDocument.Load(filePath);//加载xml文件
XElement root = xDoc.Root; //获取根节点 //遍历所有节点
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
desktop.Add(new Desktop(elem.Attribute("IpAddr").Value,elem.Attribute("Alias").Value, elem.Attribute("Pwd").Value,elem.Attribute("UserName").Value, elem.Attribute("Description").Value, new Guid(elem.Attribute("Id").Value)));
} }
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
return desktop;
} /// <summary>
/// 遍历所有节点, 获取节点的别名
/// </summary>
/// <returns></returns>
public List<string> GetNodeName()
{
List<string> nodeName = new List<string>();
try
{
this.xDoc = XDocument.Load(filePath);//加载xml文件
XElement root = xDoc.Root; //获取根节点 //遍历所有节点
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
nodeName.Add(elem.Attribute("Alias").Value);
} }
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
return nodeName;
} /// <summary>
/// 根据指定的id获取对应的节点属性
/// </summary>
/// <param name="id">指定的id</param>
/// <returns></returns>
public Desktop GetDesktop(string id)
{
this.xDoc = XDocument.Load(filePath);//加载xml文件
XElement root = xDoc.Root; //获取根节点
Desktop desktop = null;
//遍历所有节点
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
if (elem.Attribute("Id").Value == id)
{
desktop = new Desktop();
desktop.IpAddr = elem.Attribute("IpAddr").Value;
desktop.Alias = elem.Attribute("Alias").Value;
desktop.UserName = elem.Attribute("UserName").Value;
desktop.Pwd = elem.Attribute("Pwd").Value;
desktop.Description = elem.Attribute("Description").Value;
desktop.Id = new Guid(elem.Attribute("Id").Value);
}
}
return desktop;
} /// <summary>
/// 添加一个节点
/// </summary>
public void AddSave(Desktop desktop)
{
try
{
this.xDoc = XDocument.Load(this.filePath); this.xDoc.Root.Add(new XElement("Desktop",
new XAttribute("IpAddr",desktop.IpAddr),
new XAttribute("UserName", desktop.UserName),
new XAttribute("Description", desktop.Description),
new XAttribute("Pwd",desktop.Pwd),
new XAttribute("Alias", desktop.Alias),
new XAttribute("Id", desktop.Id)
));
this.xDoc.Save(this.filePath);
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
} /// <summary>
/// 根据ID来修改某个节点
/// </summary>
/// <param name="id"></param>
public void modifyNode(Desktop desktop)
{
this.xDoc = XDocument.Load(this.filePath);
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
if (elem.Attribute("Id").Value == desktop.Id.ToString())
{
elem.SetAttributeValue("IpAddr", desktop.IpAddr);
elem.SetAttributeValue("Alias", desktop.Alias);
elem.SetAttributeValue("UserName", desktop.UserName);
elem.SetAttributeValue("Pwd", desktop.Pwd);
elem.SetAttributeValue("Description", desktop.Description);
}
}
this.xDoc.Save(this.filePath);
} /// <summary>
/// 删除一个节点并保存
/// </summary>
/// <param name="desktop"></param>
public bool RemoveSave(string id)
{
bool isOK = false;
this.xDoc = XDocument.Load(this.filePath);
foreach (XElement elem in xDoc.Root.Elements("Desktop"))
{
if (elem.Attribute("Id").Value == id)
{
elem.Remove();
this.xDoc.Save(this.filePath);
isOK = true;
break;
}
}
return isOK;
}
}

xml文件示例(XMLFile.xml):

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Desktop IpAddr="host1" UserName="administrator" Description="独立服务器" Pwd="pwd" Alias="礼仪" Id="dfbc7b37-b5b3-4c36-a9c4-64302d824c34" />
<Desktop IpAddr="host2" UserName="administrator" Description="咖啡独立服务器" Pwd="pwsd" Alias="季卡" Id="80da53e6-5d84-4dc3-bb3f-90fbf0174e06" />
</Root>

C#简单操作XML的更多相关文章

  1. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  2. XML系列之--对电文格式XML的简单操作(三)

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  3. SqlServer简单的操作XML以及SQl的 try catch等统一格式

    1:SqlServer简单的操作XML: ALTER PROCEDURE [dbo].[SP_CRM_FranchiseeRecharge_Money] @Create_By VARCHAR(), @ ...

  4. php中通过DOM操作XML

    DOM文档在js里早就接触过,知道DOM不但可以操作html文档,还可以操作XHTML,XML等文档,有着极强的通用性,下面我们通过两个小例子,看看在PHP中是如何用DOM操作XML文档的,和js中差 ...

  5. php操作xml

    最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星期的基本语法后做些小练习热热身,但是期间是各种问题啊,主要是对php不熟悉, ...

  6. C#操作XML方法集合

    一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...

  7. Delphi操作XML简介

    参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...

  8. 第12章 在.NET中操作XML

    12.1 XML概述 12.1.1 为什么要有XML 12.1.2 XML文档结构 (1)文档声明 <?xml version="1.0"encoding="UTF ...

  9. XMl入门介绍及php操作XML

    一.什么是XML XML全称:Extensible Markup Language 中文名:可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标 ...

随机推荐

  1. 认证和授权(Authentication和Authorization)

    什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...

  2. Coundn't load memtrack module (No such file or directory)

    Coundn't load memtrack module (No such file or directory) 去仔细看日志,是包名有问题 一.出现症状 提示找logcat logcat里面发现C ...

  3. Python Falling back to the 'python' engine because the 'c' engine does not support regex separators

    环境 Anaconda3 Python 3.6, Window 64bit 书籍 O'Reilly出版的Wes McKinney编的<Python for Data Analysis> 警 ...

  4. 使用科大讯飞时碰上的问题(unity调用Android)

    使用科大讯飞的时候公司已经有人配置好了Android项目,由于装机量的限制,所以基本上要更换里面的资源和更改app_id,然而在unity那边调用的时候总是出现各种各样的问题,特此记录下来 1.唤醒未 ...

  5. macOS 10.12 任何来源

    sudo spctl --master-disable 从旧系统升级过来的仍然会显示“任何来源”选项,全新安装的将不再显示这个选项.可以通过上面的命令重新显示出此选项,非必要建议不要修改.

  6. datagrid拖动列头更换排列顺序

    在做这个功能的时候在网上找了大量资料,发现都不适用,要不然就是代码太冗余,所以另起炉灶,自己封装了这个函数 下面是完整的代码: <!DOCTYPE html> <html> & ...

  7. nodejs cheerio模块提取html页面内容

    nodejs cheerio模块提取html页面内容 1. nodejs cheerio模块提取html页面内容 1.1. 找到目标元素 1.2. 美化文本输出 1.3. 提取答案文本 1.4. 最终 ...

  8. WEB-INFO里面的jsp文件不能通过href 访问,而只能通过 servlet访问

    href="<%=basePath %>index.jsp"> 这种确实 可以在jsp页面跳转至另一个页面,只不过要放在WEB-INFO外面,也就是项目根目录下面 ...

  9. java网络编程TCP传输—流操作—拿到源后的写入动作

    在网络编程中的TCP传输里,拿到Socket的源后,应该怎么进行读写操作呢,下面我列举了两种方法,希望大家帮忙补充···· 1.利用byte数组作为一个缓冲区进行读写 客户端上传 //获取socket ...

  10. Unity3d 背景、音效 播放 简单demo

    仅实现功能,AudioListener在MainCamera中 using UnityEngine; using System.Collections; using System.Collection ...