代码改变世界

Linq to xml 代码实战(1)

2011-12-30 15:31  音乐让我说  阅读(356)  评论(0)    收藏  举报

直接贴代码了:

using System;

namespace ConAppTempTest
{
class Program
{
static void Main(string[] args)
{
XmlDemo.Test();
}
}
}

 

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConAppTempTest
{
public class XmlDemo
{
private static readonly string XmlFilePath = @"D:\animals.xml";

public static void Test()
{
SaveXmlToFile();
LoadXmlFile();
TestOperateXml();
RemoveXml();
}

private static void SaveXmlToFile()
{
Console.WriteLine("1. 保存 XML 文件:\n");
XDocument xdoc = new XDocument(new XElement("root",
new XElement("dog",new XAttribute("color","black"),new XCData("小狗很<可爱>,哈哈哈!")),
new XElement("cat"),
new XElement("pig", "整天谁<懒觉>,哈 哈哈!")
));

Console.WriteLine("当前用于输入到控制台的编码为:" + Console.OutputEncoding.EncodingName); //打印:简体中文(GB2312)

xdoc.Save(XmlFilePath); // 默认的 SaveOptions = None,且如果目标文件存在,则退换
//xdoc.Save(XmlFilePath, SaveOptions.DisableFormatting); // 最后只有一行文本
//xdoc.Save(XmlFilePath, SaveOptions.None);

Console.WriteLine("保存完毕!");
Console.WriteLine("\n\n");
}

private static void LoadXmlFile()
{
Console.WriteLine("2. 加载 XML 文件:\n");
XDocument xdoc = XDocument.Load(XmlFilePath);
var query = from n in xdoc.Element("root").Elements()
select new
{
Name = n.Name,
NodeType = n.NodeType,
Value = n.Value
};
foreach (var item in query)
{
Console.WriteLine("=============================");
Console.WriteLine("Name:" + item.Name);
Console.WriteLine("NodeType:" + item.NodeType);
Console.WriteLine("Value:" + item.Value);
}
Console.WriteLine("****************************************************");

Console.WriteLine("下面准备修改该 XML 文档!");
XElement rootElement = xdoc.Element("root");

rootElement.SetElementValue("tiger", "武松打虎"); // 不存在,就添加
rootElement.SetElementValue("pig", "猪肉什么的最好吃了!"); // 存在,就修改
rootElement.SetElementValue("cat", null); // 如果值为 null,则表示删除该元素

rootElement.SetAttributeValue("attribute1", "attributeValue");
// 同理 SetAttributeValue 的用法基本上和 SetElementValue 一样。
// 只是 SetElementValue 用来处理当前元素的子元素,而
// SetAttributeValue 用来处理当前元素的属性

Console.WriteLine(xdoc);
Console.WriteLine("\n\n");
}

private static void TestOperateXml()
{
Console.WriteLine("3. 测试操作 XML:\n");
XDocument xdoc = new XDocument(new XElement("root",
new XElement("child1", "child1 content"),
new XElement("child2", "child2 content"),
new XElement("child3", "child3 content"),
new XElement("child4", "child4 content")
));
XElement xChild3 = xdoc.Element("root").Element("child3");
if(xChild3 == null)
{
Console.WriteLine("xChild3为空!");
return;
}
xChild3.ReplaceWith(new XElement("newChild3", "new child3 content"));
Console.WriteLine(xdoc);
Console.WriteLine("\n\n");
}

private static void RemoveXml()
{
Console.WriteLine("4. 删除 XML 练习:\n");
XElement root = XElement.Parse(@"<Root>
<Child1>
<GrandChild1/>
<GrandChild2/>
<GrandChild3/>
</Child1>
<Child2>
<GrandChild4/>
<GrandChild5/>
<GrandChild6/>
</Child2>
<Child3>
<GrandChild7/>
<GrandChild8/>
<GrandChild9/>
</Child3>
</Root>
");
root.Element("Child1").Element("GrandChild1").Remove();
root.Element("Child2").Elements().ToList().Remove();
root.Element("Child3").Elements().Remove();
Console.WriteLine(root);
Console.WriteLine("\n\n");
}
}
}

 

 

D:\animals.xml  中的内容:

<?xml version="1.0" encoding="utf-8"?>
<root>
<dog color="black"><![CDATA[小狗很<可爱>,哈哈哈!]]></dog>
<cat />
<pig>整天谁&lt;懒觉&gt;,哈 哈哈!</pig>
</root>

 

运行截图:

谢谢浏览!