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 中文名:可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标 ...
随机推荐
- [转]Python读写文件
1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt ...
- python tesseract-ocr 基础验证码识别功能(Windows)
一.环境 windows 7 x64 Python 3 + 二.安装 1.tesseract-ocr安装 http://digi.bib.uni-mannheim.de/tesseract/ 2.py ...
- Cassandra key说明——Cassandra 整体数据可以理解成一个巨大的嵌套的Map Map<RowKey, SortedMap<ColumnKey, ColumnValue>>
Cassandra之中一共包含下面5种Key: Primary Key Partition Key Composite Key Compound Key Clustering Key 首先,Prima ...
- vi编辑器:命令模式、输入模式、末行模式
1.命令模式(command mode)—执行命令 在该模式中,可以输入命令来执行许多种功能.控制屏幕光标的移动,字符.字或行的删除,移动复制某区段及进入Insert mode下,或者到 last l ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource
二月 20, 2017 3:09:47 下午 org.apache.catalina.startup.SetAllPropertiesRule begin警告: [SetAllPropertiesRu ...
- Java学习笔记——基础篇
Tips1:eclipse中会经常用到System.out.println方法,可以先输入syso,然后eclipse就会自动联想出这个语句了!! 学习笔记: *包.权限控制 1.包(package) ...
- jdk1.8 HashMap 实现 数组+链表/红黑树
转载至 http://www.cnblogs.com/leesf456/p/5242233.html 一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Ja ...
- BZOJ - 3622:已经没有什么好害怕的了 (广义容斥)
[BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...
- python笔记-5(内置函数)
一.内置函数 1.abs()--取绝对值函数 print(abs(-0.11)) x=-0.01 y=0.11 print(abs(x),abs(y)) ----------------------- ...
- ajax传递数组后台接收不到值的问题
背景: JQGrid需要进行批量删除操作传给后台的是数组,结果后台接收不到值. 后台语言:java 原因: ajax传递参数时,traditional 默认为false,JQuery会深度序列化参数对 ...