使用XML

使用XML包括遍历文档节点列表,设置和查询属性值,创建或插入新节点。

1.基于DOM读取XML文档

System.Xml名字空间中的XmlDocument类代表了DOM。

XmlDocument类代表了一个完整的XML文档,它本身也是一个XmlNode。

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <items>
  3.   <item id="1" name="Flaming Sword of Doom"
  4.         description="This sword will vanquish all of your foes with a single swipe">
  5.     <attribute name="attack" value="10" />
  6.     <attribute name="weight" value="20" />
  7.   </item>
  8.   <item id="2" name="Bag of Really Big Stuff"
  9.         description="this bag can hold a lot of stuff.">
  10.     <attribute name="weight" value="1" />
  11.     <attribute name="capacity" value="80" />
  12.   </item>
  13.   <item id="3" name="Broach of Bug Smashing"
  14.         description="This broach will kill any bug. Instantly.">
  15.     <attribute name="weight" value="1" />
  16.     <attribute name="attack" value="11" />
  17.     <specials>
  18.       <special name="killbug" description="This thing kills any bug instantly." />
  19.     </specials>
  20.   </item>
  21.   <item id="4" name="Wand of Traffic Vanquish"
  22.         description="A single wave of this wand will part the highway before you.">
  23.     <attribute name="weight" value="5" />
  24.     <attribute name="attack" value="20" />
  25.     <specials>
  26.       <special name="parttraffic" description="All vehicles move out of your way." />
  27.     </specials>
  28.   </item>
  29. </items>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. using System.IO;
  6. namespace MemStream
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             XmlDocument itemDoc = new XmlDocument();
  13.             if (File.Exists(@"../../items.xml"))
  14.             {
  15.                 itemDoc.Load(@"../../items.xml");
  16.                 Console.WriteLine("Document has {0} children", itemDoc.DocumentElement.ChildNodes.Count);
  17.                 foreach (XmlNode itemNode in itemDoc.DocumentElement.ChildNodes)
  18.                 {
  19.                     //we know that the node is an element, so we can do this;
  20.                     XmlElement itemElement = (XmlElement)itemNode;
  21.                     Console.WriteLine("/n[Item]: {0}/n{1}", itemElement.Attributes["name"].Value,
  22.                         itemElement.Attributes["description"].Value);
  23.                     if (itemNode.ChildNodes.Count == 0)
  24.                     {
  25.                         Console.WriteLine("No additional Information");
  26.                     }
  27.                     else
  28.                     {
  29.                         foreach (XmlNode childNode in itemNode.ChildNodes)
  30.                         {
  31.                             if (childNode.Name.ToUpper() == "ATTRIBUTE")
  32.                             {
  33.                                 Console.WriteLine("{0} : {1}",
  34.                                     childNode.Attributes["name"].Value,
  35.                                     childNode.Attributes["value"].Value);
  36.                             }
  37.                             else if (childNode.Name.ToUpper() == "SEPCIALS")
  38.                             {
  39.                                 foreach (XmlNode specialNode in childNode)
  40.                                 {
  41.                                     Console.WriteLine("{0} : {1}"
  42.                                         specialNode.Attributes["name"].Value,
  43.                                         specialNode.Attributes["description"].Value);
  44.                                 }
  45.                             }
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine("No file");
  53.             }
  54.         }
  55.     }
  56. }

 

2.用Xpath查询XML文档

  1.             XmlDocument itemsDoc = new XmlDocument();
  2.             itemsDoc.Load(@"../../items.xml");
  3.             XmlNodeList allItems = itemsDoc.SelectNodes("/items/item");
  4.             Console.WriteLine("count: {0}", allItems.Count);
  5.             XmlNode thirdItem = itemsDoc.SelectSingleNode("/items/item[2]");
  6.             Console.WriteLine("Third node is {0}", thirdItem.Attributes["name"].Value);

3.用XSLT转换XML文档

XSLT结合了XPath语言和一组转换函数。一般将XML文档转换成XHTML文档。

要进行转换需要一个XSLT文档。

  1. XslCompiledTransform xct = new XslCompiledTransform();
  2. xct.Load(@"../../itemsTransform.xslt");
  3. xct.Transform(@"../../items.xml""items.html");
4.用XSD确认XML文档

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值