网站运营seo文章大全提供全面的站长运营经验及seo技术!//
//bool xmltextreader.read(): 读取流中下一个节点,当读完最后一个节点再次调用该方法该方法返回false
//xmlnodetype xmltextreader.nodetype: 该属性返回当前节点的类型
// xmlnodetype.element 元素节点
// xmlnodetype.endelement 结尾元素节点
// xmlnodetype.xmldeclaration 文档的第一个节点
// xmlnodetype.text 文本节点
//bool xmltextreader.hasattributes: 当前节点有没有属性,返回true或false
//string xmltextreader.name: 返回当前节点的名称
//string xmltextreader.value: 返回当前节点的值
//string xmltextreader.localname: 返回当前节点的本地名称
//string xmltextreader.namespaceuri: 返回当前节点的命名空间uri
//string xmltextreader.prefix: 返回当前节点的前缀
//bool xmltextreader.movetonextattribute(): 移动到当前节点的下一个属性
//---------------------------------------------------------------------------------------------------
namespace xmlreading
{
using system;
using system.xml;
using system.windows.forms;
using system.componentmodel;
/// <summary>
/// xml文件读取器
/// </summary>
public class xmlreader : idisposable
{
private string _xmlpath;
private const string _errmsg = "error occurred while reading ";
private listbox _listbox;
private xmltextreader xmltxtrd;
#region xmlreader 的构造器
public xmlreader()
{
this._xmlpath = string.empty;
this._listbox = null;
this.xmltxtrd = null;
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="_xmlpath">xml文件绝对路径</param>
/// <param name="_listbox">列表框用于显示xml</param>
public xmlreader(string _xmlpath, listbox _listbox)
{
this._xmlpath = _xmlpath;
this._listbox = _listbox;
this.xmltxtrd = null;
}
#endregion
#region xmlreader 的资源释放方法
/// <summary>
/// 清理该对象所有正在使用的资源
/// </summary>
public void dispose()
{
this.dispose(true);
gc.suppressfinalize(this);
}
/// <summary>
/// 释放该对象的实例变量
/// </summary>
/// <param name="disposing"></param>
protected virtual void dispose(bool disposing)
{
if (!disposing)
return;
if (this.xmltxtrd != null)
{
this.xmltxtrd.close();
this.xmltxtrd = null;
}
if (this._xmlpath != null)
{
this._xmlpath = null;
}
}
#endregion
#region xmlreader 的属性
/// <summary>
/// 获取或设置列表框用于显示xml
/// </summary>
public listbox listbox
{
get
{
return this._listbox;
}
set
{
this._listbox = value;
}
}
/// <summary>
/// 获取或设置xml文件的绝对路径
/// </summary>
public string xmlpath
{
get
{
return this._xmlpath;
}
set
{
this._xmlpath = value;
}
}
#endregion
/// <summary>
/// 遍历xml文件
/// </summary>
public void eachxml()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
this._listbox.items.add(this.xmltxtrd.value);
}
}
catch(xmlexception exp)
{
throw new xmlexception(_errmsg + this._xmlpath + exp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的节点类型
/// </summary>
public void readxmlbynodetype()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
this._listbox.items.add(this.xmltxtrd.nodetype.tostring());
}
}
catch(xmlexception exp)
{
throw new xmlexception(_errmsg + this._xmlpath + exp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 根据节点类型过滤xml文档
/// </summary>
/// <param name="xmlntype">xmlnodetype 节点类型的数组</param>
public void filterbynodetype(xmlnodetype[] xmlntype)
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
for (int i = 0; i < xmlntype.length; i++)
{
if (xmltxtrd.nodetype == xmlntype[i])
{
this._listbox.items.add(xmltxtrd.name + " is type " + xmltxtrd.nodetype.tostring());
}
}
}
}
catch(xmlexception exp)
{
throw new xmlexception(_errmsg + this.xmlpath + exp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的所有文本节点值
/// </summary>
public void readxmltextvalue()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.text)
{
this._listbox.items.add(xmltxtrd.value);
}
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的属性
/// </summary>
public void readxmlattributes()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.element)
{
if (xmltxtrd.hasattributes)
{
this._listbox.items.add("the element " + xmltxtrd.name + " has " + xmltxtrd.attributecount + " attributes");
this._listbox.items.add("the attributes are:");
while(xmltxtrd.movetonextattribute())
{
this._listbox.items.add(xmltxtrd.name + " = " + xmltxtrd.value);
}
}
else
{
this._listbox.items.add("the element " + xmltxtrd.name + " has no attribute");
}
this._listbox.items.add("");
}
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的命名空间
/// </summary>
public void readxmlnamespace()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.element && xmltxtrd.prefix != "")
{
this._listbox.items.add("the prefix " + xmltxtrd.prefix + " is associated with namespace " + xmltxtrd.namespaceuri);
this._listbox.items.add("the element with the local name " + xmltxtrd.localname + " is associated with" + " the namespace " + xmltxtrd.namespaceuri);
}
if (xmltxtrd.nodetype == xmlnodetype.element && xmltxtrd.hasattributes)
{
while(xmltxtrd.movetonextattribute())
{
if (xmltxtrd.prefix != "")
{
this._listbox.items.add("the prefix " + xmltxtrd.prefix + " is associated with namespace " + xmltxtrd.namespaceuri);
this._listbox.items.add("the attribute with the local name " + xmltxtrd.localname + " is associated with the namespace " + xmltxtrd.namespaceuri);
}
}
}
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取整个xml文件
/// </summary>
public void readxml()
{
string attandele = string.empty;
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.xmldeclaration)
this._listbox.items.add(string.format("<?{0} {1} ?>",xmltxtrd.name,xmltxtrd.value));
else if (xmltxtrd.nodetype == xmlnodetype.element)
{
attandele = string.format("<{0} ",xmltxtrd.name);
if (xmltxtrd.hasattributes)
{
while(xmltxtrd.movetonextattribute())
{
attandele = attandele + string.format("{0}='{1}' ",xmltxtrd.name,xmltxtrd.value);
}
}
attandele = attandele.trim() + ">";
this._listbox.items.add(attandele);
}
else if (xmltxtrd.nodetype == xmlnodetype.endelement)
this._listbox.items.add(string.format("</{0}>",xmltxtrd.name));
else if (xmltxtrd.nodetype == xmlnodetype.text)
this._listbox.items.add(xmltxtrd.value);
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
}
}
窗体代码如下:
namespace xmlreading
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.xml;
public class form1 : system.windows.forms.form
{
private system.windows.forms.listbox listbox1;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.button button3;
private system.windows.forms.button button4;
private system.windows.forms.button button5;
private system.windows.forms.button button6;
private system.windows.forms.button button7;
private string xmlpath;
private xmlreader xread;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
initializecomponent();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.listbox1 = new system.windows.forms.listbox();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.button5 = new system.windows.forms.button();
this.button6 = new system.windows.forms.button();
this.button7 = new system.windows.forms.button();
this.suspendlayout();
//
// listbox1
//
this.listbox1.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom)
| system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.listbox1.itemheight = 12;
this.listbox1.location = new system.drawing.point(8, 8);
this.listbox1.name = "listbox1";
this.listbox1.size = new system.drawing.size(716, 460);
this.listbox1.tabindex = 0;
//
// button1
//
this.button1.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button1.location = new system.drawing.point(8, 488);
this.button1.name = "button1";
this.button1.tabindex = 1;
this.button1.text = "example1";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button2.location = new system.drawing.point(96, 488);
this.button2.name = "button2";
this.button2.tabindex = 2;
this.button2.text = "example2";
this.button2.click += new system.eventhandler(this.button2_click);
//
// button3
//
this.button3.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
this.button3.location = new system.drawing.point(648, 488);
this.button3.name = "button3";
this.button3.tabindex = 3;
this.button3.text = "example7";
this.button3.click += new system.eventhandler(this.button3_click);
//
// button4
//
this.button4.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button4.location = new system.drawing.point(184, 488);
this.button4.name = "button4";
this.button4.tabindex = 4;
this.button4.text = "example3";
this.button4.click += new system.eventhandler(this.button4_click);
//
// button5
//
this.button5.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button5.location = new system.drawing.point(272, 488);
this.button5.name = "button5";
this.button5.tabindex = 5;
this.button5.text = "example4";
this.button5.click += new system.eventhandler(this.button5_click);
//
// button6
//
this.button6.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button6.location = new system.drawing.point(360, 488);
this.button6.name = "button6";
this.button6.tabindex = 6;
this.button6.text = "example5";
this.button6.click += new system.eventhandler(this.button6_click);
//
// button7
//
this.button7.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button7.location = new system.drawing.point(448, 488);
this.button7.name = "button7";
this.button7.tabindex = 7;
this.button7.text = "example6";
this.button7.click += new system.eventhandler(this.button7_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(728, 517);
this.controls.add(this.button7);
this.controls.add(this.button6);
this.controls.add(this.button5);
this.controls.add(this.button4);
this.controls.add(this.button3);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.controls.add(this.listbox1);
this.name = "form1";
this.text = "xmlreader";
this.resumelayout(false);
//
// xmlpath
//
this.xmlpath = "sample.xml";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
xread = new xmlreader(this.xmlpath,this.listbox1);
try
{
xread.eachxml();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
private void button2_click(object sender, system.eventargs e)
{
xread = new xmlreader(this.xmlpath,this.listbox1);
try
{
xread.readxmlbynodetype();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
private void button3_click(object sender, system.eventargs e)
{
xmlnodetype[] xmlntype = {xmlnodetype.element, xmlnodetype.endelement, xmlnodetype.xmldeclaration};
xread = new xmlreader(this.xmlpath, this.listbox1);
try
{
xread.filterbynodetype(xmlntype);
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
private void button4_click(object sender, system.eventargs e)
{
xread = new xmlreader(this.xmlpath, this.listbox1);
try
{
xread.readxmltextvalue();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
private void button5_click(object sender, system.eventargs e)
{
xread = new xmlreader(this.xmlpath, this.listbox1);
try
{
xread.readxmlattributes();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
private void button6_click(object sender, system.eventargs e)
{
xread = new xmlreader(this.xmlpath, this.listbox1);
try
{
xread.readxmlnamespace();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
private void button7_click(object sender, system.eventargs e)
{
xread = new xmlreader(this.xmlpath, this.listbox1);
try
{
xread.readxml();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
finally
{
xread.dispose();
}
}
}
}
回答者:
≡vip≡ - 同进士出身 六级 12-20 22:25
//
//bool xmltextreader.read(): 读取流中下一个节点,当读完最后一个节点再次调用该方法该方法返回false
//xmlnodetype xmltextreader.nodetype: 该属性返回当前节点的类型
// xmlnodetype.element 元素节点
// xmlnodetype.endelement 结尾元素节点
// xmlnodetype.xmldeclaration 文档的第一个节点
// xmlnodetype.text 文本节点
//bool xmltextreader.hasattributes: 当前节点有没有属性,返回true或false
//string xmltextreader.name: 返回当前节点的名称
//string xmltextreader.value: 返回当前节点的值
//string xmltextreader.localname: 返回当前节点的本地名称
//string xmltextreader.namespaceuri: 返回当前节点的命名空间uri
//string xmltextreader.prefix: 返回当前节点的前缀
//bool xmltextreader.movetonextattribute(): 移动到当前节点的下一个属性
//---------------------------------------------------------------------------------------------------
namespace xmlreading
{
using system;
using system.xml;
using system.windows.forms;
using system.componentmodel;
/// <summary>
/// xml文件读取器
/// </summary>
public class xmlreader : idisposable
{
private string _xmlpath;
private const string _errmsg = "error occurred while reading ";
private listbox _listbox;
private xmltextreader xmltxtrd;
#region xmlreader 的构造器
public xmlreader()
{
this._xmlpath = string.empty;
this._listbox = null;
this.xmltxtrd = null;
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="_xmlpath">xml文件绝对路径</param>
/// <param name="_listbox">列表框用于显示xml</param>
public xmlreader(string _xmlpath, listbox _listbox)
{
this._xmlpath = _xmlpath;
this._listbox = _listbox;
this.xmltxtrd = null;
}
#endregion
#region xmlreader 的资源释放方法
/// <summary>
/// 清理该对象所有正在使用的资源
/// </summary>
public void dispose()
{
this.dispose(true);
gc.suppressfinalize(this);
}
/// <summary>
/// 释放该对象的实例变量
/// </summary>
/// <param name="disposing"></param>
protected virtual void dispose(bool disposing)
{
if (!disposing)
return;
if (this.xmltxtrd != null)
{
this.xmltxtrd.close();
this.xmltxtrd = null;
}
if (this._xmlpath != null)
{
this._xmlpath = null;
}
}
#endregion
#region xmlreader 的属性
/// <summary>
/// 获取或设置列表框用于显示xml
/// </summary>
public listbox listbox
{
get
{
return this._listbox;
}
set
{
this._listbox = value;
}
}
/// <summary>
/// 获取或设置xml文件的绝对路径
/// </summary>
public string xmlpath
{
get
{
return this._xmlpath;
}
set
{
this._xmlpath = value;
}
}
#endregion
/// <summary>
/// 遍历xml文件
/// </summary>
public void eachxml()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
this._listbox.items.add(this.xmltxtrd.value);
}
}
catch(xmlexception exp)
{
throw new xmlexception(_errmsg + this._xmlpath + exp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的节点类型
/// </summary>
public void readxmlbynodetype()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
this._listbox.items.add(this.xmltxtrd.nodetype.tostring());
}
}
catch(xmlexception exp)
{
throw new xmlexception(_errmsg + this._xmlpath + exp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 根据节点类型过滤xml文档
/// </summary>
/// <param name="xmlntype">xmlnodetype 节点类型的数组</param>
public void filterbynodetype(xmlnodetype[] xmlntype)
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
for (int i = 0; i < xmlntype.length; i++)
{
if (xmltxtrd.nodetype == xmlntype[i])
{
this._listbox.items.add(xmltxtrd.name + " is type " + xmltxtrd.nodetype.tostring());
}
}
}
}
catch(xmlexception exp)
{
throw new xmlexception(_errmsg + this.xmlpath + exp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的所有文本节点值
/// </summary>
public void readxmltextvalue()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.text)
{
this._listbox.items.add(xmltxtrd.value);
}
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的属性
/// </summary>
public void readxmlattributes()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.element)
{
if (xmltxtrd.hasattributes)
{
this._listbox.items.add("the element " + xmltxtrd.name + " has " + xmltxtrd.attributecount + " attributes");
this._listbox.items.add("the attributes are:");
while(xmltxtrd.movetonextattribute())
{
this._listbox.items.add(xmltxtrd.name + " = " + xmltxtrd.value);
}
}
else
{
this._listbox.items.add("the element " + xmltxtrd.name + " has no attribute");
}
this._listbox.items.add("");
}
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取xml文件的命名空间
/// </summary>
public void readxmlnamespace()
{
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.element && xmltxtrd.prefix != "")
{
this._listbox.items.add("the prefix " + xmltxtrd.prefix + " is associated with namespace " + xmltxtrd.namespaceuri);
this._listbox.items.add("the element with the local name " + xmltxtrd.localname + " is associated with" + " the namespace " + xmltxtrd.namespaceuri);
}
if (xmltxtrd.nodetype == xmlnodetype.element && xmltxtrd.hasattributes)
{
while(xmltxtrd.movetonextattribute())
{
if (xmltxtrd.prefix != "")
{
this._listbox.items.add("the prefix " + xmltxtrd.prefix + " is associated with namespace " + xmltxtrd.namespaceuri);
this._listbox.items.add("the attribute with the local name " + xmltxtrd.localname + " is associated with the namespace " + xmltxtrd.namespaceuri);
}
}
}
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
/// <summary>
/// 读取整个xml文件
/// </summary>
public void readxml()
{
string attandele = string.empty;
this._listbox.items.clear();
this.xmltxtrd = new xmltextreader(this._xmlpath);
try
{
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == xmlnodetype.xmldeclaration)
this._listbox.items.add(string.format("<?{0} {1} ?>",xmltxtrd.name,xmltxtrd.value));
else if (xmltxtrd.nodetype == xmlnodetype.element)
{
attandele = string.format("<{0} ",xmltxtrd.name);
if (xmltxtrd.hasattributes)
{
while(xmltxtrd.movetonextattribute())
{
attandele = attandele + string.format("{0}='{1}' ",xmltxtrd.name,xmltxtrd.value);
}
}
attandele = attandele.trim() + ">";
this._listbox.items.add(attandele);
}
else if (xmltxtrd.nodetype == xmlnodetype.endelement)
this._listbox.items.add(string.format("</{0}>",xmltxtrd.name));
else if (xmltxtrd.nodetype == xmlnodetype.text)
this._listbox.items.add(xmltxtrd.value);
}
}
catch(xmlexception xmlexp)
{
throw new xmlexception(_errmsg + this._xmlpath + xmlexp.tostring());
}
finally
{
if (this.xmltxtrd != null)
this.xmltxtrd.close();
}
}
}
}
窗体代码如下:
namespace xmlreading
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.xml;
public class form1 : system.windows.forms.form
{
private system.windows.forms.listbox listbox1;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.button button3;
private system.windows.forms.button button4;
private system.windows.forms.button button5;
private system.windows.forms.button button6;
private system.windows.forms.button button7;
private string xmlpath;
private xmlreader xread;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
initializecomponent();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.listbox1 = new system.windows.forms.listbox();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.button5 = new system.windows.forms.button();
this.button6 = new system.windows.forms.button();
this.button7 = new system.windows.forms.button();
this.suspendlayout();
//
// listbox1
//
this.listbox1.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom)
| system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.listbox1.itemheight = 12;
this.list