使用xmltextreader对xml进行简单的读取和写入操作

类别:.NET开发 点击:0 评论:0 推荐:

使用xmltextreader对xml文件进行简单的以对象形式的读取和写入操作

BaseSerializeElement

-------------------------------------------------------------

using System;
using System.Xml;
using System.Collections;

namespace Local.Framework.Common
{
 /// <summary>
 /// BaseXmlSerializer 的摘要说明。
 /// --fallseir.li 20050427 15:12
 /// </summary>
 public class BaseSerializeElement
 {
  public BaseSerializeElement()
  {
  }
  public BaseSerializeElement(XmlReader reader,bool isroot)
  {
   this._isroot=isroot;
   ReadXml(reader,isroot);
  }
  public BaseSerializeElement(XmlReader reader):this(reader,false){}

  #region attributes
  public bool IsRoot
  {
   get{return _isroot;}
  }
  private bool _isroot;
  public string TAG
  {
   get{return _tag;}
  }
  private string _tag;
  public string Value
  {
   get{return _value;}
  }
  public string _value;
  public string CData
  {
   get{return _cdata;}
  }
  private string _cdata;
  public ArrayList Elements
  {
   get{return _elements;}
  }
  private ArrayList _elements;
  
  public Hashtable Attributes
  {
   get
   {
    return _attributes;
   }
  }
  private Hashtable _attributes;

  private string _prefix;
  private string _namespaceuri;
  private string _xmllang;
  //private string _xmlspace;
        private string _baseuri;
  //private XmlNameTable _nametable;

  //root
  private string _declaration;
  private string _documenttype;

  #endregion

  public void AddAttribute(string key,string value)
  {
   if(_attributes==null)
    _attributes=new Hashtable();
   if(_attributes.ContainsKey(key))
   {
    _attributes[key]=value;
   }
   else
   {
    _attributes.Add(key,value);
   }
  }
  public void AddElement(BaseSerializeElement element)
  {
   if(_elements==null)
    _elements=new ArrayList();
   _elements.Add(element);
  }

  #region IXmlSerializable 成员

  public void WirteXmlRoot(XmlWriter writer)
  {
   writeRoot(writer);
   WriteXml(writer);
  }
  private void writeRoot(XmlWriter writer)
  {
   writer.WriteProcessingInstruction("xml",this._declaration);
//   writer.WriteDocType
  }
  public void WriteXml(XmlWriter writer)
  {
   if(this._prefix!="")
    writer.WriteStartElement(this._prefix+":"+TAG);
   else
    writer.WriteStartElement(TAG);
   //if(this._nametable!=null)
    
   if(Attributes!=null)
   {
    foreach(object key in Attributes.Keys)
    {
     writer.WriteAttributeString(key as string,Attributes[key] as string);
    }
   }
   if(Value!=null)
   {
    writer.WriteString(Value);
   }
   else
   {
    if(Elements!=null)
    {
     foreach(object item in Elements)
     {
      BaseSerializeElement inner=(BaseSerializeElement)item;
      inner.WriteXml(writer);
     }
    }
   }
   writer.WriteEndElement();
  }
//
//  public System.Xml.Schema.XmlSchema GetSchema()
//  {
//   // TODO:  添加 AnyElement.GetSchema 实现
//   return null;
//  }
  public void ReadXml(XmlReader reader)
  {
   ReadXml(reader,false);
  }
  private void ReadRoot(XmlReader reader)
  {
   while(reader.Read())
   {
    if(reader.EOF)
     return; 
    switch(reader.NodeType)
    {
     case XmlNodeType.XmlDeclaration:
      this._isroot=true;
      this._declaration=reader.Value;
      break;
//     case XmlNodeType.Comment:
//      AddComment(reader.Value);
//      break;
     case XmlNodeType.DocumentType:
      this._documenttype=reader.Value;
      break;
     case XmlNodeType.Element:
      return;
     default:
      break;
    }
    
   }
  }
//  public void AddComment(string comment)
//  {
//   _comments.Add(comment);
//  }
  public void ReadXml(System.Xml.XmlReader reader,bool isroot)
  {
   if(reader.NodeType==XmlNodeType.None||isroot)
   {
    ReadRoot(reader);
   }

   if(reader.EOF||reader.NodeType!=XmlNodeType.Element)
    return;
   
   this._tag=reader.LocalName;
   this._prefix=reader.Prefix;
   this._namespaceuri=reader.NamespaceURI;
   this._xmllang=reader.XmlLang;
   //this._xmlspace=reader.XmlSpace;
   this._baseuri=reader.BaseURI;
   //this._nametable=reader.NameTable;
   int depth=reader.Depth;
   bool isempty=reader.IsEmptyElement;
   if(reader.HasAttributes)
   { 
    int n=reader.AttributeCount;
    for(int i=0;i<n;i++)
    {
     reader.MoveToAttribute(i);
     AddAttribute(reader.Name,reader.Value);  
    }
   }
   if(isempty)
    return;
   //读取所有内部xml元素
   while(reader.Read())
   {
    switch(reader.NodeType)
    {
     case XmlNodeType.EndElement:
      if((reader.Depth==depth)&&(reader.Name==TAG))
       return;
      break;
     case XmlNodeType.Element:
      if(reader.Depth==depth+1)
       AddElement(new BaseSerializeElement(reader));
      break;
     case XmlNodeType.Text:
      if(reader.Depth==depth+1)
       _value=reader.Value;
      break;
     case XmlNodeType.CDATA:
      if(reader.Depth==depth+1)
       _cdata=reader.Value;
      break;
    }    
   }   
  }

  #endregion
  private void print(string format,params object[] args)
  {
   Console.WriteLine(format,args);
  }
 }
}

tester
----------------------

using System;
using System.IO;
using System.Xml;

using LocalPostBB.Framework.Common;

namespace LocalPostBB
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Tester
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   Tester tester=new Tester();
   tester.TestSerialize();
   Console.ReadLine();
  }
  public void TestSerialize()
  {
   BaseSerializeElement t=null;
   string filename=@"E:\Program Files\Altova\XMLSpy2005\Examples\OrgChartDebug.xslt";
   XmlTextReader reader=new XmlTextReader(filename);
   
   t=new BaseSerializeElement(reader,true);
   reader.Close();
   
   StringWriter wr=new StringWriter();
   XmlTextWriter writer=new XmlTextWriter(wr);
   writer.Formatting=Formatting.Indented;  
   t.WirteXmlRoot(writer);
   Console.WriteLine(wr.ToString());
   writer.Close();
   Console.WriteLine("successful!");
  }
 }
}
------------------

初步代码 只对xml文件进行了简单的读写功能

本文地址:http://com.8s8s.com/it/it41072.htm