using System;
using System.Xml;// 为了能使用XML操作,我们必须应用System.Xml的命名空间
using System.IO;
namespace OperationXML
{
public class OperationXML
{
public OperationXML ()
{
}
public static void CreateXML()
{
// 为了操作XML文件,我们需要一个总控制对象,这个控制对象就是类似我们的文件句柄
XmlDocument pDocument = new XmlDocument ();// 这个总控制对象我们可以直接实例化
// 创建标准的XML头,用来指派XML中的版本号和XML的编解码的方式,还可以限定他的系统平台。
XmlDeclaration pD = pDocument.CreateXmlDeclaration ("1.0", "UTF-8", null);// 这个标准头必须通过我们的XmlDocument来创建,而且别去指定平台限制,standonlong为 null就表示全平台
// 生产的标准头必须回转回来表示XML的解析模式;
pDocument.AppendChild (pD);
// XML必须用一个总的节点把所有表述信息的节点圈起来,让他们在总节点范围内运动,这样方便我们查找所有的子节点;
// 这个方便我们查找子节点的节点对象,我们称为根节点
// 根节点也是节点,只是我们人为的认为他是根节点而已,他并不特殊
XmlElement pRoot = pDocument.CreateElement ("root");
pDocument.AppendChild (pRoot);
// XML中需要注意节点的追加关系,对于根节点需要追加给我们的XmlDocument对象,对于根节点之下的节点他们就产生从属关系,由他自己父节点来控制以下的节点
XmlElement pNode = pDocument.CreateElement ("name");// 对于子节点我们的生产过程还是要依靠我们的总XmlDocument来完成。
// 从属关系从此开始改变了,他的父节点是人为指派的,所以谁去AppendChild当前子节点那么这个子节点的父节点就是调用AppendChild的节点对象;
pRoot.AppendChild (pNode);
XmlElement pSubNode = pDocument.CreateElement ("SubNode");
pNode.AppendChild (pSubNode);
// 对于节点中的信息添加,我们需要使用字符串形式,没有任何的数据类型的区分,全部都是字符串
pSubNode.InnerText = "1.0001";// 在字符串中我们写浮点的时候不要去写最后的f 这样的形式,如果你写了在转换成真浮点数时会转换报错!
// XML节点是可以支持属性添加的,用来表示当前节点的一些特定信息
XmlAttribute pAttr = pDocument.CreateAttribute ("dateType");
pAttr.Value = "float";// 属性创建的时候只能指派属性名,但是属性必须要有值才能说是完整的,那么我们可以对属性的Value设置属性值,这个Value必须是字符串型的,同样没有数据类型区分;
// 属性构建出来了他是独立,但是他不能真的独立存在必须依附在一个指定的节点身上。每个节点可以有多个属性,所以每个节点的属性一定是列表级的
pSubNode.Attributes.Append (pAttr);
string path = Directory.GetCurrentDirectory() + "/1.xml";
pDocument.Save (path);
}
public static void ReadXML()
{
string path = Directory.GetCurrentDirectory() + "/1.xml";
// 对于XML的解析我们同样需要我们的XmlDocument来完成
XmlDocument pDocument = new XmlDocument ();
pDocument.Load (path); // XmlDocument可以通过Load方法直接读取指定绝对路径的文件;
// 对于XML中的标准解析方案,我们可以忽略,解析器自己会判断,我们只关心根节点,因为我们可以通过根节点获取所有的节点元素,包括节点中的属性和内容;
XmlElement pRoot = pDocument.DocumentElement;// 因为根节点是添加到我们的XmlDocument所以作为根XmlDocument就一定会为我们保留根节点对象
XmlNodeList pChileNodes = pRoot.ChildNodes;// 每个节点下的子节点可以有无数个,所以一般我们的子节点的存储一定会使用列表形式的。
if (pChileNodes.Count > 0) {// 我们可以通过XmlNodeList的Count来检测子节点的数量
foreach (XmlNode pNode in pChileNodes) {
System.Console.WriteLine(pNode.Name);
if (pNode.ChildNodes.Count > 0) {
XmlNodeList pList = pNode.ChildNodes;
foreach (XmlNode pSubNode in pList) {
System.Console.WriteLine("SubNode Name: {0}", pSubNode.Name);
if (pSubNode.Attributes.Count > 0) {// 检测到他存在属性
XmlAttributeCollection pAttrs = pSubNode.Attributes;
foreach(XmlAttribute pA in pAttrs) {
System.Console.WriteLine("SubNode Attrbute: {0}, Value: {1}", pA.Name, pA.Value);
}
}
}
}
}
}
// 注意:以上的方法是最2B的方式了,所以我们可以用比较先进的技术代替他,我们参见下面的方法吧!
}
//
public static void OpXML()
{
string path = Directory.GetCurrentDirectory() + "/Books.xml";
XmlDocument pDocument = new XmlDocument ();
pDocument.Load (path);
XmlElement root = pDocument.DocumentElement;
// 先了解增关系
XmlElement theBook = pDocument.CreateElement ("book");
root.AppendChild (theBook);
XmlElement theName = pDocument.CreateElement ("name");
theName.InnerText = "失乐园";
theBook.AppendChild (theName);
XmlElement thePrice = pDocument.CreateElement ("price");
thePrice.InnerText = string.Format("{0}", 20);
theBook.AppendChild (thePrice);
XmlElement theMemo = pDocument.CreateElement ("memo");
theMemo.InnerText = "男主人公和一个小三的悲催的结局";
theBook.AppendChild (theMemo);
// 索引XML中的指定数据的节点
theBook = root.SelectSingleNode ("/books/book[name='哈里波特']") as XmlElement;
if (theBook != null) {
System.Console.WriteLine("theBook Price: {0}", theBook.GetElementsByTagName("price")[0].InnerText);
}
// 通过属性查找制定对象
theBook = root.SelectSingleNode ("/books/book[@id='B04']") as XmlElement;
if (theBook != null) {
System.Console.WriteLine("theBook Name: {0}", theBook.GetElementsByTagName("name")[0].InnerText);
theBook.GetElementsByTagName("memo")[0].InnerText = "一个男人如何驾驭这么多女人的故事";
}
// 删除一本没有意思的故事书
theBook = root.SelectSingleNode ("/books/book[name='三国演义']") as XmlElement;
if (theBook != null) {
System.Console.WriteLine("theBook Name: {0}", theBook.GetElementsByTagName("name")[0].InnerText);
theBook.ParentNode.RemoveChild(theBook);
theBook = null;
}
path = Directory.GetCurrentDirectory() + "/Books1.xml";
MemoryStream pBuffer = new MemoryStream ();
pDocument.Save (pBuffer);
byte[] pData = pBuffer.GetBuffer ();
pBuffer.Close ();
string str = System.Text.Encoding.UTF8.GetString (pData);
System.Console.WriteLine (str);
}
}// end class
}// end namespace
XML
最新推荐文章于 2024-04-14 19:48:05 发布