• 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>


  • 相关阅读:
    解决:微信小程序富文本识别不了空白p标签的方法
    微信小程序
    fastadmin
    微信小程序
    fastadmin使用笔记
    fastadmin使用笔记
    您有新的订单,请注意查收 提示音
    js获取视频第一帧生成图片
    解决JQ WeUI 的 Picker无法动态传值的问题
    微擎 人人商城小程序获取不到用户信息
  • 原文地址:https://www.cnblogs.com/tenghoo/p/1231701.html
Copyright © 2020-2023  润新知