ASP.NET XML高级编程(二、XMLReader类和XMLWriter类)

第二章 XMLReader 类和XMLWriter

2.1读取XMLXMLReader

XMLReader是面向流的,它把XML文档看做是文本数据流。

XMLReader是一个抽象类。

XMLReader使用pull模式处理流。

。三个派生类:XMLTextReaderXMLNodeReaderXMLValidatingReader

 XMLTextReader:把XML当做文本流来处理,不需要高速缓存、实体解析或验证,要求XML文档是格式良好的。

 XMLNodeReader:根据XMLNode的实例处理XML

 XMLValidatingReader:类似于XMLTextReader,但验证DTDXDR模式或XSD模式。

2.1.1XMLTextReader

   1.构造函数

   XMLTextReader提供了众多构造函数的重载。

   XMLTextReader(),XMLTextReader(Stream),XMLTextReader(String)

   2.属性

   30个属性:NameItemValueNoteType

   3.方法

   30个方法:ReadGetAttributeReadStringMoveToNextAttribute

2.2编写XMLXMLWriter

   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使用实例
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();
            }

        }

    }
调用
     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文件
<? 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 >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值