XmlReader提供对XML 数据进行快速、非缓存、只进访问的读取器。创建XML 读取器可以使用Create 方法和XmlReaderSettings 类,可以指定要在所创建的XmlReader 对象上支持的功能,XmlReaderSettings 类可以重复使用,以创建多个读取器对象。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
namespace UsingDataSet
{
public partial class Form1 : Form
{
string strSQL = null;
DataSet ds = null;
public Form1()
{
InitializeComponent();
}
private void readFromFileToolStripMenuItem_Click(object sender, EventArgs e)
{
//创建XmlRader
XmlReaderSettings set = new XmlReaderSettings();
set.ConformanceLevel = ConformanceLevel.Document;
set.IgnoreWhitespace = true;
set.IgnoreComments = true;
XmlReader read = XmlReader.Create(@"D:\Study\XmlControl\UsingXml\Product.Xml", set);
read.Read();
read.Read();
MessageBox.Show(read.ReadInnerXml());
read.Read();
read.Close();
}
//数据库返回XmlReader对象
private void rToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlReader xr = SqlHelper.ExecuteXmlReader(CommandType.StoredProcedure, "usp_GetProductsXmlXPath");
xr.Read();
MessageBox.Show(xr.ReadOuterXml());
xr.Close();
}
}
}