一 .Net框架中与XML有关的定名空间
System.Xml
包含了一些和XML文档的读写操纵相干的类,它们分别是:XmlReader、XmlTextReader、XmlValidatingReader、XmlNodeReader、XmlWriter、XmlTextWriter 以及 XmlNode(它的子类包含:XmlDocument、XmlDataDocument、XmlDocumentFragment)等类。
System.Xml.Schema
包含了和XML形式相干的类,这些类包含XmlSchema、XmlSchemaAll、XmlSchemaXPath以及XmlSchemaType等类。
System.Xml.Serialization
包含了和XML文档的序列化和反序列化操纵相干的类。
序列化:将XML花样的数据转化为流花样的数据,并能在收集中传输;
反序列化:完成相反的操纵,行将流花样的数据还原成XML花样的数据。
System.Xml.Xpath
包含了XPathDocument、XPathExression、XPathNavigator以及XPathNodeIterator等类,这些类能完成XML文档的导航功用。
(在XPathDocument类的辅佐下,XPathNavigator类能完成疾速的XML文档导航功用,该类为顺序员供应了很多Move要领以完成导航功用。)
System.Xml.Xsl
完成XSLT的转换功用。
二 写XML文档的要领
用XmlWriter类完成写操纵,该类包含了写XML文档所需的要领和属性,它是XmlTextWriter类和XmlNodeWriter类的基类。
写操纵的有些要领是成对涌现的,比方你要写入一个元素,起首挪用WriteStartElement要领—>写入现实内容—>挪用WriteEndElement要领完毕。
下面经由历程其子类 XmlTextWriter 来申明怎样写XML文档。
XmlTextWriter textWriter = New XmlTextWriter("C:\\myXmFile.xml", null);
在建立完对象后,我们挪用WriterStartDocument要领最先写XML文档;
在完成写事情后,就挪用WriteEndDocument完毕写历程,并挪用Close要领将它封闭。
在写的历程当中,我们能够:
挪用WriteComment要领来增加申明;
经由历程挪用WriteString要领来增加一个字符串;
经由历程挪用WriteStartElement和WriteEndElement要领对来增加一个元素;
经由历程挪用WriteStartAttribute和WriteEndAttribute要领对来增加一个属性;
经由历程挪用WriteNode要领来增加整的一个节点;
别的的写的要领还包含WriteProcessingInstruction和WriteDocType等等。
下面的示例引见怎样细致应用这些要领来完成XML文档的写事情。
代码以下:
using System; using System.Xml; namespace WriteXML { class Class1 { static void Main( string[] args ) { try { // 建立XmlTextWriter类的实例对象 XmlTextWriter textWriter = new XmlTextWriter("C:\\w3sky.xml", null); textWriter.Formatting = Formatting.Indented; // 最先写历程,挪用WriteStartDocument要领 textWriter.WriteStartDocument(); // 写入申明 textWriter.WriteComment("First Comment XmlTextWriter Sample Example"); textWriter.WriteComment("w3sky.xml in root dir"); //建立一个节点 textWriter.WriteStartElement("Administrator"); textWriter.WriteElementString("Name", "formble"); textWriter.WriteElementString("site", "w3sky.com"); textWriter.WriteEndElement(); // 写文档完毕,挪用WriteEndDocument要领 textWriter.WriteEndDocument(); // 封闭textWriter textWriter.Close(); } catch(System.Exception e) { Console.WriteLine(e.ToString()); } } } }
三 读XML文档的要领
用XmlTextReader类的对象来读取该XML文档。在建立新对象的组织函数中指明XML文件的位置即可。
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
XmlTextReader 类中的属性 NodeType 能够晓得其节点的节点范例。经由历程与罗列范例 XmlNodeType 中的元素的比较,能够猎取响应节点的节点范例并对其完成相干的操纵。
罗列范例 XmlNodeType 中包含了诸如XmlDeclaration、Attribute、CDATA、Element、Comment、Document、DocumentType、Entity、ProcessInstruction以及WhiteSpace等XML项的范例。
下面的示例是以读取"books.xml"文件建立对象,经由历程该xml对象的Name、BaseURI、Depth、LineNumber等属性来猎取相干信息,并显现在掌握台中。(应用VS.net开发工具附带的"books.xml"文件来作为示例)
代码以下:
using System; using System.Xml; namespace ReadXml { class Class1 { static void Main( string[] args ) { // 建立一个XmlTextReader类的对象并挪用Read要领来读取XML文件 XmlTextReader textReader = new XmlTextReader("C:\\books.xml"); textReader.Read(); // 节点非空则实行循环体 while ( textReader.Read() ) { // 读取第一个元素 textReader.MoveToElement(); Console.WriteLine("XmlTextReader Properties Test"); Console.WriteLine("==================="); // 读取该元素的属性并显现在掌握台中 Console.WriteLine("Name:" + textReader.Name); Console.WriteLine("Base URI:" + textReader.BaseURI); Console.WriteLine("Local Name:" + textReader.LocalName); Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString()); Console.WriteLine("Depth:" + textReader.Depth.ToString()); Console.WriteLine("Line Number:" + textReader.LineNumber.ToString()); Console.WriteLine("Node Type:" + textReader.NodeType.ToString()); Console.WriteLine("Attribute Count:" + textReader.Value.ToString()); } } } }
四 应用XmlDocument类
XmlDocument类代表了XML文档,它能完成与全部XML文档相干的各种操纵,同时和其相干的XmlDataDocument类也是非常主要的,值得深入研究。 该类包含了Load、LoadXml以及Save等主要的要领。
Load要领: 能够从一个字符串指定的XML文件或是一个流对象、一个TextReader对象、一个XmlReader对象导入XML数据。
LoadXml要领: 则完成从一个特定的XML文件导入XML数据的功用。
Save要领: 则将XML数据保存到一个XML文件中或是一个流对象、一个TextWriter对象、一个XmlWriter对象中。
下面的示例中,用到了XmlDocument类对象的LoadXml要领,它从一个XML文档段中读取XML数据并挪用其Save要领将数据保存在一个文件中。
代码以下:
// 建立一个XmlDocument类的对象 XmlDocument doc = new XmlDocument(); doc.LoadXml(("<Student type='regular' Section='B'><Name>Tommy Lex</Name></Student>")); // 保存到文件中 doc.Save("C:\\student.xml"); // 还能够经由历程转变Save要领中参数,将XML数据显现在掌握台中,要领以下: doc.Save(Console.Out); 下面的示例中,用到了一个XmlTextReader对象,经由历程它读取"books.xml"文件中的XML数据。然后建立一个XmlDocument对象并载入XmlTextReader对象,如许XML数据就被读到XmlDocument对象中了。末了,经由历程该对象的Save要领将XML数据显现在掌握台中。 XmlDocument doc = new XmlDocument(); // 建立一个XmlTextReader对象,读取XML数据 XmlTextReader reader = new XmlTextReader("c:\\books.xml"); reader.Read(); // 载入XmlTextReader类的对象 doc.Load(reader); // 将XML数据显现在掌握台中 doc.Save(Console.Out);
xml文件
代码以下:
<?xml version='1.0'?> <!-- This file represents a fragment of a book store inventory database --> <bookstore> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <first-name>Sidas</first-name> <last-name>Plato</last-name> </author> <price>9.99</price> </book> </bookstore>
别的一个.net操纵xml文件示例
代码以下:
//设置配置文件物理途径 public string xmlPath = "/manage/spider/config.xml"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //设置顺序物理途径+文件物理途径 string path = Request.PhysicalApplicationPath + xmlPath; //猎取XML元素对象 XElement config = XElement.Load(path); if (config != null) { //取得节点子元素 XElement eleAmazonDetailUrl = config.Element("AmazonDetailUrl"); XElement eleAmazonListUrl = config.Element("AmazonListUrl"); XElement eleHz = config.Element("Hz"); XElement eleCount = config.Element("Count"); //在页面上显现取到的数据 if (eleAmazonDetailUrl != null) TextBox_AmazonDetailUrl.Text = eleAmazonDetailUrl.Value; if (eleAmazonListUrl != null) TextBox_AmazonListUrl.Text = eleAmazonListUrl.Value; if (eleHz != null) TextBox_Hz.Text = eleHz.Value; if (eleCount != null) TextBox_Count.Text = eleCount.Value; } else Response.Write(""); } } protected void btn_Save_Click(object sender, EventArgs e) { //设置XML文件途径 string path = Request.PhysicalApplicationPath + xmlPath; //设置节点的称号和内容 XElement root = new XElement("Settings", new XElement("AmazonDetailUrl", TextBox_AmazonDetailUrl.Text.Trim()), new XElement("AmazonListUrl", TextBox_AmazonListUrl.Text.Trim()), new XElement("Hz", TextBox_Hz.Text.Trim()), new XElement("Count", TextBox_Count.Text.Trim()) ); //将元素序列化到指定途径的XML文件当中 root.Save(path); }
以上就是.net读写xml文档详解的细致内容,更多请关注ki4网别的相干文章!