使用XML包括遍历文档节点列表,设置和查询属性值,创建或插入新节点。
1.基于DOM读取XML文档
System.Xml名字空间中的XmlDocument类代表了DOM。
XmlDocument类代表了一个完整的XML文档,它本身也是一个XmlNode。
- <?xml version="1.0" encoding="utf-8" ?>
- <items>
- <item id="1" name="Flaming Sword of Doom"
- description="This sword will vanquish all of your foes with a single swipe">
- <attribute name="attack" value="10" />
- <attribute name="weight" value="20" />
- </item>
- <item id="2" name="Bag of Really Big Stuff"
- description="this bag can hold a lot of stuff.">
- <attribute name="weight" value="1" />
- <attribute name="capacity" value="80" />
- </item>
- <item id="3" name="Broach of Bug Smashing"
- description="This broach will kill any bug. Instantly.">
- <attribute name="weight" value="1" />
- <attribute name="attack" value="11" />
- <specials>
- <special name="killbug" description="This thing kills any bug instantly." />
- </specials>
- </item>
- <item id="4" name="Wand of Traffic Vanquish"
- description="A single wave of this wand will part the highway before you.">
- <attribute name="weight" value="5" />
- <attribute name="attack" value="20" />
- <specials>
- <special name="parttraffic" description="All vehicles move out of your way." />
- </specials>
- </item>
- </items>
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- using System.IO;
- namespace MemStream
- {
- class Program
- {
- static void Main(string[] args)
- {
- XmlDocument itemDoc = new XmlDocument();
- if (File.Exists(@"../../items.xml"))
- {
- itemDoc.Load(@"../../items.xml");
- Console.WriteLine("Document has {0} children", itemDoc.DocumentElement.ChildNodes.Count);
- foreach (XmlNode itemNode in itemDoc.DocumentElement.ChildNodes)
- {
- //we know that the node is an element, so we can do this;
- XmlElement itemElement = (XmlElement)itemNode;
- Console.WriteLine("/n[Item]: {0}/n{1}", itemElement.Attributes["name"].Value,
- itemElement.Attributes["description"].Value);
- if (itemNode.ChildNodes.Count == 0)
- {
- Console.WriteLine("No additional Information");
- }
- else
- {
- foreach (XmlNode childNode in itemNode.ChildNodes)
- {
- if (childNode.Name.ToUpper() == "ATTRIBUTE")
- {
- Console.WriteLine("{0} : {1}",
- childNode.Attributes["name"].Value,
- childNode.Attributes["value"].Value);
- }
- else if (childNode.Name.ToUpper() == "SEPCIALS")
- {
- foreach (XmlNode specialNode in childNode)
- {
- Console.WriteLine("{0} : {1}",
- specialNode.Attributes["name"].Value,
- specialNode.Attributes["description"].Value);
- }
- }
- }
- }
- }
- }
- else
- {
- Console.WriteLine("No file");
- }
- }
- }
- }
2.用Xpath查询XML文档
- XmlDocument itemsDoc = new XmlDocument();
- itemsDoc.Load(@"../../items.xml");
- XmlNodeList allItems = itemsDoc.SelectNodes("/items/item");
- Console.WriteLine("count: {0}", allItems.Count);
- XmlNode thirdItem = itemsDoc.SelectSingleNode("/items/item[2]");
- Console.WriteLine("Third node is {0}", thirdItem.Attributes["name"].Value);
3.用XSLT转换XML文档
XSLT结合了XPath语言和一组转换函数。一般将XML文档转换成XHTML文档。
要进行转换需要一个XSLT文档。
- XslCompiledTransform xct = new XslCompiledTransform();
- xct.Load(@"../../itemsTransform.xslt");
- xct.Transform(@"../../items.xml", "items.html");