使用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文件进行了简单的读写功能