ASP.NET XML高级编程(二、XMLReader类和XMLWriter类)
第二章 XMLReader类和XMLWriter类
2.1读取XML:XMLReader
。XMLReader是面向流的,它把XML文档看做是文本数据流。
。XMLReader是一个抽象类。
。XMLReader使用pull模式处理流。
。三个派生类:XMLTextReader、XMLNodeReader和XMLValidatingReader
XMLTextReader:把XML当做文本流来处理,不需要高速缓存、实体解析或验证,要求XML文档是格式良好的。
XMLNodeReader:根据XMLNode的实例处理XML。
XMLValidatingReader:类似于XMLTextReader,但验证DTD,XDR模式或XSD模式。
2.1.1XMLTextReader
1.构造函数
XMLTextReader提供了众多构造函数的重载。
XMLTextReader(),XMLTextReader(Stream),XMLTextReader(String)等
2.属性
近30个属性:Name,Item,Value,NoteType等
3.方法
近30个方法:Read,GetAttribute,ReadString,MoveToNextAttribute等
2.2编写XML:XMLWriter
。XMLWriter是一个抽象类
。派生类:XMLTextWriter
XMLWriter使用实例
class WriteXML
{
public void WriteTextXml(string url) {
XmlTextWriter xmldoc = null;
try
{
xmldoc = new XmlTextWriter(url, System.Text.Encoding.UTF8);
xmldoc.WriteStartDocument();
xmldoc.WriteStartElement("info");
xmldoc.WriteStartElement("OPERATION");
xmldoc.WriteStartAttribute("ACTION");
xmldoc.WriteString("ADD");
xmldoc.WriteEndAttribute();
xmldoc.WriteStartElement("USER-MODULE");
xmldoc.WriteStartElement("EMPLOYEE-GUID");
xmldoc.WriteString("employee2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("DEPARTMENT-GUID");
xmldoc.WriteString("department2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_LOGINNAME");
xmldoc.WriteString("loginname2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_EMAIL");
xmldoc.WriteString("email2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_MOBILE");
xmldoc.WriteString("mobile2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_BIRTHDAY");
xmldoc.WriteString("birthday2");
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
}
catch (XmlException exc) { }
finally {
if (xmldoc != null)
xmldoc.Close();
}
}
}
XMLReader使用实例{
public void WriteTextXml(string url) {
XmlTextWriter xmldoc = null;
try
{
xmldoc = new XmlTextWriter(url, System.Text.Encoding.UTF8);
xmldoc.WriteStartDocument();
xmldoc.WriteStartElement("info");
xmldoc.WriteStartElement("OPERATION");
xmldoc.WriteStartAttribute("ACTION");
xmldoc.WriteString("ADD");
xmldoc.WriteEndAttribute();
xmldoc.WriteStartElement("USER-MODULE");
xmldoc.WriteStartElement("EMPLOYEE-GUID");
xmldoc.WriteString("employee2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("DEPARTMENT-GUID");
xmldoc.WriteString("department2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_LOGINNAME");
xmldoc.WriteString("loginname2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_EMAIL");
xmldoc.WriteString("email2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_MOBILE");
xmldoc.WriteString("mobile2");
xmldoc.WriteEndElement();
xmldoc.WriteStartElement("EMPLOYEE_BIRTHDAY");
xmldoc.WriteString("birthday2");
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
}
catch (XmlException exc) { }
finally {
if (xmldoc != null)
xmldoc.Close();
}
}
}
class ReadXML
{
public void XmlTextRead(string url)
{
XmlTextReader xmldoc = null;
try
{
xmldoc = new XmlTextReader(url);
xmldoc.WhitespaceHandling = WhitespaceHandling.None;
while (xmldoc.Read())
{
switch (xmldoc.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", xmldoc.Name);
break;
case XmlNodeType.Text:
Console.Write(xmldoc.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", xmldoc.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1} ?>", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", xmldoc.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(xmldoc.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", xmldoc.Name);
break;
}
}
}
catch (XmlException exc)
{
}
finally {
if (xmldoc != null)
xmldoc.Close();
}
}
public void XMLValidatingRead(string url)
{
XmlTextReader xmlreader = null;
XmlValidatingReader xmldoc = null;
try
{
xmlreader = new XmlTextReader(url);
xmlreader.WhitespaceHandling = WhitespaceHandling.None;
xmldoc = new XmlValidatingReader(xmlreader);
while (xmldoc.Read())
{
switch (xmldoc.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", xmldoc.Name);
break;
case XmlNodeType.Text:
Console.Write(xmldoc.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", xmldoc.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1} ?>", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", xmldoc.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(xmldoc.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", xmldoc.Name);
break;
}
}
}
catch (XmlException exc)
{
}
finally {
if (xmlreader != null)
xmlreader.Close();
if (xmldoc != null)
xmldoc.Close();
}
}
public void XMLNodeRead(XmlNode node) {
XmlNodeReader xmldoc = null;
try {
xmldoc = new XmlNodeReader(node);
while (xmldoc.Read())
{
switch (xmldoc.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", xmldoc.Name);
break;
case XmlNodeType.Text:
Console.Write(xmldoc.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", xmldoc.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1} ?>", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", xmldoc.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(xmldoc.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", xmldoc.Name);
break;
}
}
}
catch (XmlException exc)
{
}
finally
{
if (xmldoc != null)
xmldoc.Close();
}
}
}
调用{
public void XmlTextRead(string url)
{
XmlTextReader xmldoc = null;
try
{
xmldoc = new XmlTextReader(url);
xmldoc.WhitespaceHandling = WhitespaceHandling.None;
while (xmldoc.Read())
{
switch (xmldoc.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", xmldoc.Name);
break;
case XmlNodeType.Text:
Console.Write(xmldoc.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", xmldoc.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1} ?>", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", xmldoc.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(xmldoc.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", xmldoc.Name);
break;
}
}
}
catch (XmlException exc)
{
}
finally {
if (xmldoc != null)
xmldoc.Close();
}
}
public void XMLValidatingRead(string url)
{
XmlTextReader xmlreader = null;
XmlValidatingReader xmldoc = null;
try
{
xmlreader = new XmlTextReader(url);
xmlreader.WhitespaceHandling = WhitespaceHandling.None;
xmldoc = new XmlValidatingReader(xmlreader);
while (xmldoc.Read())
{
switch (xmldoc.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", xmldoc.Name);
break;
case XmlNodeType.Text:
Console.Write(xmldoc.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", xmldoc.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1} ?>", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", xmldoc.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(xmldoc.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", xmldoc.Name);
break;
}
}
}
catch (XmlException exc)
{
}
finally {
if (xmlreader != null)
xmlreader.Close();
if (xmldoc != null)
xmldoc.Close();
}
}
public void XMLNodeRead(XmlNode node) {
XmlNodeReader xmldoc = null;
try {
xmldoc = new XmlNodeReader(node);
while (xmldoc.Read())
{
switch (xmldoc.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", xmldoc.Name);
break;
case XmlNodeType.Text:
Console.Write(xmldoc.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", xmldoc.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1} ?>", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", xmldoc.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", xmldoc.Name, xmldoc.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(xmldoc.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", xmldoc.Name);
break;
}
}
}
catch (XmlException exc)
{
}
finally
{
if (xmldoc != null)
xmldoc.Close();
}
}
}
class Program
{
static void Main(string[] args)
{
//-----XMLRead---------
//ReadXML readxml = new ReadXML();
//readxml.XmlTextRead("F:\\XML\\employee2.xml");
//readxml.XMLValidatingRead("F:\\XML\\employee2.xml");
//XmlDocument xml = new XmlDocument();
//xml.Load("F:\\XML\\employee.xml");
//readxml.XMLNodeRead(xml.DocumentElement);
//----XMLWrite------------
WriteXML writexml = new WriteXML();
writexml.WriteTextXml("F:\\XML\\employee2.xml");
Console.ReadLine();
}
}
XML文件{
static void Main(string[] args)
{
//-----XMLRead---------
//ReadXML readxml = new ReadXML();
//readxml.XmlTextRead("F:\\XML\\employee2.xml");
//readxml.XMLValidatingRead("F:\\XML\\employee2.xml");
//XmlDocument xml = new XmlDocument();
//xml.Load("F:\\XML\\employee.xml");
//readxml.XMLNodeRead(xml.DocumentElement);
//----XMLWrite------------
WriteXML writexml = new WriteXML();
writexml.WriteTextXml("F:\\XML\\employee2.xml");
Console.ReadLine();
}
}
<?xml version="1.0" encoding="utf-8"?>
<info>
<OPERATION ACTION="ADD">
<USER-MODULE>
<EMPLOYEE-GUID>employee2</EMPLOYEE-GUID>
<DEPARTMENT-GUID>department2</DEPARTMENT-GUID>
<EMPLOYEE_LOGINNAME>loginname2</EMPLOYEE_LOGINNAME>
<EMPLOYEE_EMAIL>email2</EMPLOYEE_EMAIL>
<EMPLOYEE_MOBILE>mobile2</EMPLOYEE_MOBILE>
<EMPLOYEE_BIRTHDAY>birthday2</EMPLOYEE_BIRTHDAY>
</USER-MODULE>
</OPERATION>
</info>
<info>
<OPERATION ACTION="ADD">
<USER-MODULE>
<EMPLOYEE-GUID>employee2</EMPLOYEE-GUID>
<DEPARTMENT-GUID>department2</DEPARTMENT-GUID>
<EMPLOYEE_LOGINNAME>loginname2</EMPLOYEE_LOGINNAME>
<EMPLOYEE_EMAIL>email2</EMPLOYEE_EMAIL>
<EMPLOYEE_MOBILE>mobile2</EMPLOYEE_MOBILE>
<EMPLOYEE_BIRTHDAY>birthday2</EMPLOYEE_BIRTHDAY>
</USER-MODULE>
</OPERATION>
</info>
作者:青羽