C#简单操作XML
类文件:
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的更多相关文章
- Linq对XML的简单操作
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- XML系列之--对电文格式XML的简单操作(三)
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- SqlServer简单的操作XML以及SQl的 try catch等统一格式
1:SqlServer简单的操作XML: ALTER PROCEDURE [dbo].[SP_CRM_FranchiseeRecharge_Money] @Create_By VARCHAR(), @ ...
- php中通过DOM操作XML
DOM文档在js里早就接触过,知道DOM不但可以操作html文档,还可以操作XHTML,XML等文档,有着极强的通用性,下面我们通过两个小例子,看看在PHP中是如何用DOM操作XML文档的,和js中差 ...
- php操作xml
最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星期的基本语法后做些小练习热热身,但是期间是各种问题啊,主要是对php不熟悉, ...
- C#操作XML方法集合
一 前言 先来了解下操作XML所涉及到的几个类及之间的关系 如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...
- Delphi操作XML简介
参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...
- 第12章 在.NET中操作XML
12.1 XML概述 12.1.1 为什么要有XML 12.1.2 XML文档结构 (1)文档声明 <?xml version="1.0"encoding="UTF ...
- XMl入门介绍及php操作XML
一.什么是XML XML全称:Extensible Markup Language 中文名:可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标 ...
随机推荐
- BZOJ 3329 Xorequ:数位dp + 矩阵快速幂
传送门 题意 现有如下方程:$ x \oplus 3x = 2x $ 其中 $ \oplus $ 表示按位异或. 共 $ T $ 组数据,每组数据给定正整数 $ n $,任务如下: 求出小于等于 $ ...
- Python jieba 分词
环境 Anaconda3 Python 3.6, Window 64bit 目的 利用 jieba 进行分词,关键词提取 代码 # -*- coding: utf-8 -*- import jieba ...
- 第一章 Burp Suite 安装和环境配置
Burp Suite是一个集成化的渗透测试工具,它集合了多种渗透测试组件,使我们自动化地或手工地能更好的完成对web应用的渗透测试和攻击.在渗透测试中,我们使用Burp Suite将使得测试工作变得更 ...
- 【python】imp模块的使用
是import在程序中的使用 [一]函数load_source imp.load_source(moduleName, sourceFile) 使用: abc = imp.load_source('d ...
- flash播放器插件与flash播放器的区别
flash插件是一个网页ActiveX控件,而flash播放器是一个exe的可执行程序.前者用于播放网页中的falsh动画,而后者用于播放本地swf格式文件.
- [转载]Python注册表信息丢失的解决方案
今天安装Python的模块时,安装失败,提示信息:Python version 2.7 required, which was not found in the registry. 原因在于Pytho ...
- 跟我学Delphi Xe4 开发 IOS 一 , 重读Delphi Xe4 自带文档.
安装了 Delphi Xe4 之后打开这个地址就是完整的官方的文档了. 虽然不是立刻能解决你的问题. 但也是必须要看一遍的. 最基础的都在这里了. ms-help://embarcadero.rs_x ...
- Ant入门: Hello World with Apache Ant
笔者因项目需要进行java程序打包,之前一直使用的最多的打包工具要数fat-jar了.此工具将所有引用的jar包以及源码生成的class一起打到一个包里面,运行程序的时候直接运行命令:java –ja ...
- Bootstrap和IE何时能相亲相爱啊~
公司新项目,嘚瑟了一下,用了用Bootstrap... ... 发现了一个小坑(也许只是对我而言)... ... 使用了2.x的Jquery,在chrome等高版本浏览器一切顺利... ... 然,3 ...
- description方法
1.NSLog回顾 众所周知,我们可以用NSLog函数来输出字符串和一些基本数据类 1 int age = 11; 2 NSLog(@"age is %i", age); * 第2 ...