您的位置:首页 > 编程语言 > C#

C#对XMl文件的操作

2016-02-07 11:19 399 查看
C# 对xml文件的操作比较多,例如,当我们设计一款软件,需要保存软件的设置信息,这个时候,可以把参数以xml文件的形式进行存储。便于下次打开软件时,以上次的设置值打开。又例如,在网络通信的时候,我们写的软件要与其它服务器进行通信,以xml形式通信,这个时候也是需要用到这个知识。又例如,我们常见的PC桌面浏览器软件,也是直接接收的xml文件,最后通过浏览器软件处理成好看的网页内容。这些说明了学会用C#操作xml文件是相当普遍和重要的。以下这里总结的知识,便于后面方便查找跟使用。

一.以下是xml文件的查找

方法1:一般方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace xml查找
{
class Program
{
static void Main(string[] args)
{
XDocument xdoc = XDocument.Load("person.xml");
List<XElement> list = new List<XElement>();
SearchElementsZhao(xdoc.Root,list);

}
public static void SearchElementsZhao(XElement ele,List<XElement> list)
{
foreach(XElement item in ele.Elements())
{
//判断这个元素的名字是不是name,如果是name 那么看一下名字是不是赵小虎
if(item.Name.LocalName=="name")
{
if(item.Value=="赵晓虎")
{
list.Add(item.Parent);
}
}
SearchElementsZhao(item, list);//递归

}
}
}
}

方法2:linq查询语法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace linq_xml
{
class Program
{
static void Main(string[] args)
{
XDocument xdoc = XDocument.Load("person.xml");
//  一个是linq查询语法(与sql语句类似)
var query = from s in xdoc.Descendants()
where s.Name.LocalName == "name" && s.Value == "赵晓虎"
select s.Parent;

foreach (XElement item in query)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}


person.xml文件内容如下

<?xml version="1.0" encoding="gb2312"?>


注意:这里开头的语句另外一种形式是

<?xml version="1.0" encoding="UTF-8"?>
关于这两种的区别待下回分解

<?xml version="1.0" encoding="UTF-8"?>

-<root>

-<person 测试属性="测试数据" id="1">

<name>赵晓虎</name>

<sex>男</sex>

<age>44</age>

</person>

+<person 测试属性="测试数据" id="2">

-<person 测试属性="测试数据" id="3">

<name>马伦</name>

<sex>男</sex>

<age>60</age>

</person>

-<person 测试属性="测试数据" id="4">

<name>杨洪波</name>

<sex>女</sex>

<age>35</age>

</person>

-<person 测试属性="测试数据" id="5">

<name>赵剑宇(黑皮)</name>

<sex>男</sex>

<age>59</age>

</person>

-<test>

-<tt>

-<ee>

<name>赵晓虎</name>

</ee>

</tt>

</test>

</root>
二.以下是xml文件的写入:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace _04使用LinqToXML
{
class Program
{
static void Main(string[] args)
{
// System.XML.Linq;
// XDocument
// XElement
// XAttribute

// 兼容做法
#region 兼容传统的语法
XDocument xDoc = new XDocument();
// xDoc.Declaration = new XDeclaration(
// XElement xRoot = new XElement("root", "我是一个文本内容");
XElement xRoot = new XElement("root");
xRoot.Value = "我是文本";

XAttribute xAttr = new XAttribute("Id", "0002");
xDoc.Add(xRoot);
xRoot.Add(xAttr);
xDoc.Save("2.xml");
#endregion

// 真正Linq的语法
#region Linq语法演示
// F#  函数式编程语言
// 基于函数式
//new XDocument(
//        new XElement("root",
//                new XAttribute("id", "12345"),
//                "我是一个根节点"
//            )
//    ).Save("3.xml");
// f1().f2().f3()....
// 链式编程,流水线生产
// lisp
#endregion

#region 一个案例
string[] strs = "赵晓虎,牛亮亮,马伦,杨洪波,赵剑宇(黑皮)".Split(',');
Random r = new Random();

XDocument xdoc = new XDocument(new XElement("root"));
// xdoc.Root
for (int i = 0; i < strs.Length; i++)
{
xdoc.Root.Add(new XElement("person",
new XAttribute("id", i + 1),
new XAttribute("测试属性", "测试数据"),
new XElement("name", strs[i]),
new XElement("sex", "男女"[r.Next(2)]),
new XElement("age", r.Next(18, 65))
));
}

xdoc.Save("person.xml");
#endregion
#region 最简便的方法
XDocument.Parse(@"<?xml version=""1.0"" encoding=""gb2312""?>
<root>
<person id=""123"">测试</person>
</root>").Save("4.xml");
#endregion
}
}
}
生成的person.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>

-<root>

-<person 测试属性="测试数据" id="1">

<name>赵晓虎</name>

<sex>男</sex>

<age>44</age>

</person>

-<person 测试属性="测试数据" id="2">

<name>牛亮亮</name>

<sex>男</sex>

<age>25</age>

</person>

-<person 测试属性="测试数据" id="3">

<name>马伦</name>

<sex>男</sex>

<age>60</age>

</person>

-<person 测试属性="测试数据" id="4">

<name>杨洪波</name>

<sex>女</sex>

<age>35</age>

</person>

-<person 测试属性="测试数据" id="5">

<name>赵剑宇(黑皮)</name>

<sex>男</sex>

<age>59</age>

</person>

</root>
生成的4.xml文件如下:

<?xml version="1.0" encoding="gb2312"?>
<root>
<person id="123">测试</person>
</root>


未完待续,还需要对xml的文件进行修改和查找
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: