?? util.cs
字號:
using System;
using System.Web;
//using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Schema;
using System.Data;
using System.IO;
/// <summary>
/// 提供公共函數(shù)和方法供整個應用程序使用
/// </summary>
public class Util
{
private static Object lockObj = new Object();
/// <summary>
/// 當xml驗證發(fā)生錯誤時的回調(diào)函數(shù)
/// </summary>
private static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
lock (lockObj)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
break;
case XmlSeverityType.Warning:
break;
}
}
}
/// <summary>
/// 驗證Xml文件是否格式良好,確定配置文件。
/// </summary>
private static void ValidateXml(string xmlFilePath, string schemaFilePath)
{
//創(chuàng)建一個XmlSchemaSet,并且將指定路徑的Schema文件添加到settings.Schemas.
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add(null, schemaFilePath);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(schema);
// 解析XML數(shù)據(jù)文件。并用指定的Sechema進行驗證。
using (XmlReader reader = XmlReader.Create(xmlFilePath, settings))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
xmlDoc.Validate(eventHandler);
}
}
/// <summary>
/// 讀取xmlFilePath中的XML文件,并使用指定的方案文件進行驗證。并返回包含了XML文件數(shù)據(jù)的DataSet.
/// </summary>
public static DataSet ReadAndValidateXml(string xmlFilePath, string schemaFilePath)
{
DataSet dataSet = null;
//先對該文件和Schema進行驗證操作。
Util.ValidateXml(xmlFilePath, schemaFilePath);
using (FileStream fs_xml = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read))
{
using (FileStream fs_xsd = new FileStream(schemaFilePath, FileMode.Open, FileAccess.Read))
{
//調(diào)用DataSet的ReadXmlSchema方法首先讀取Secham,再加載數(shù)據(jù)。
dataSet = new DataSet();
dataSet.ReadXmlSchema(fs_xsd);
dataSet.ReadXml(fs_xml, XmlReadMode.IgnoreSchema);
}
}
//返回該DataSet.
return dataSet;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -