在Visual C#中使用XML指南之读取XML
//---------------------------------------------------------------------------------------------------
//XmlReader类用于Xml文件的一般读取操作,以下对这个类做简单介绍:
//
//Attributes(属性):
//listBox: 设置该属性主要为了得到客户端控件以便于显示所读到的文件的内容(这里是ListBox控件)
//xmlPath: 设置该属性为了得到一个确定的Xml文件的绝对路径
//
//Basilic Using(重要的引用):
//System.Xml: 该命名空间中封装有对Xml进行操作的常用类,本类中使用了其中的XmlTextReader类
//XmlTextReader: 该类提供对Xml文件进行读取的功能,它可以验证文档是否格式良好,如果不是格式 // 良好的Xml文档,该类在读取过程中将会抛出XmlException异常,可使用该类提供的
// 一些方法对文档节点进行读取,筛选等操作以及得到节点的名称和值
//
//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();
}
}
}
}
//XmlReader类用于Xml文件的一般读取操作,以下对这个类做简单介绍:
//
//Attributes(属性):
//listBox: 设置该属性主要为了得到客户端控件以便于显示所读到的文件的内容(这里是ListBox控件)
//xmlPath: 设置该属性为了得到一个确定的Xml文件的绝对路径
//
//Basilic Using(重要的引用):
//System.Xml: 该命名空间中封装有对Xml进行操作的常用类,本类中使用了其中的XmlTextReader类
//XmlTextReader: 该类提供对Xml文件进行读取的功能,它可以验证文档是否格式良好,如果不是格式 // 良好的Xml文档,该类在读取过程中将会抛出XmlException异常,可使用该类提供的
// 一些方法对文档节点进行读取,筛选等操作以及得到节点的名称和值
//
//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();
}
}
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。