第二章 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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void WriteTextXml(string url)
{
XmlTextWriter xmldoc = null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xmldoc = new XmlTextWriter(url, System.Text.Encoding.UTF8);
xmldoc.WriteStartDocument();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("info");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("OPERATION");
xmldoc.WriteStartAttribute("ACTION");
xmldoc.WriteString("ADD");
xmldoc.WriteEndAttribute();
xmldoc.WriteStartElement("USER-MODULE");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("EMPLOYEE-GUID");
xmldoc.WriteString("employee2");
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("DEPARTMENT-GUID");
xmldoc.WriteString("department2");
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("EMPLOYEE_LOGINNAME");
xmldoc.WriteString("loginname2");
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("EMPLOYEE_EMAIL");
xmldoc.WriteString("email2");
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("EMPLOYEE_MOBILE");
xmldoc.WriteString("mobile2");
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteStartElement("EMPLOYEE_BIRTHDAY");
xmldoc.WriteString("birthday2");
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
xmldoc.WriteEndElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch (XmlException exc)
{ }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{
if (xmldoc != null)
xmldoc.Close();
}
}
}
XMLReader使用实例
class ReadXML
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public void XmlTextRead(string url)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlTextReader xmldoc = null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xmldoc = new XmlTextReader(url);
xmldoc.WhitespaceHandling = WhitespaceHandling.None;
while (xmldoc.Read())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (xmldoc.NodeType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{
if (xmldoc != null)
xmldoc.Close();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
public void XMLValidatingRead(string url)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlTextReader xmlreader = null;
XmlValidatingReader xmldoc = null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xmlreader = new XmlTextReader(url);
xmlreader.WhitespaceHandling = WhitespaceHandling.None;
xmldoc = new XmlValidatingReader(xmlreader);
while (xmldoc.Read())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (xmldoc.NodeType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch (XmlException exc)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{
if (xmlreader != null)
xmlreader.Close();
if (xmldoc != null)
xmldoc.Close();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void XMLNodeRead(XmlNode node)
{
XmlNodeReader xmldoc = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
xmldoc = new XmlNodeReader(node);
while (xmldoc.Read())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (xmldoc.NodeType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (xmldoc != null)
xmldoc.Close();
}
}
}
调用
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//-----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");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.ReadLine();
}
}
XML文件
<?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>