Bạn có thể tạo cá thể XmlReader xác thực bằng cách sử dụng lớp XmlReaderSettings và phương thức Tạo.
private bool ValidateXml(string xmlFilePath, string schemaFilePath, string schemaNamespace, Type rootType)
{
XmlSerializer serializer = new XmlSerializer(rootType);
using (var fs = new StreamReader(xmlFilePath, Encoding.GetEncoding("iso-8859-1")))
{
object deserializedObject;
var xmlReaderSettings = new XmlReaderSettings();
if (File.Exists(schemaFilePath))
{
//select schema for validation
xmlReaderSettings.Schemas.Add(schemaNamespace, schemaPath);
xmlReaderSettings.ValidationType = ValidationType.Schema;
try
{
using (var xmlReader = XmlReader.Create(fs, xmlReaderSettings))
{
if (serializer.CanDeserialize(xmlReader))
{
return true;
//deserializedObject = serializer.Deserialize(xmlReader);
}
else
{
return false;
}
}
}
catch(Exception ex)
{ return false; }
}
}
}
Mã trên sẽ ném ngoại lệ nếu lược đồ không hợp lệ hoặc không thể deserialize xml. rootType là loại phần tử gốc trong phân cấp lớp tương đương.
Ví dụ: Schema tại địa chỉ: XML Schema Tutorial. Lưu tệp dưới dạng D:\SampleSchema.xsd
.
Run xsd.exe
:
- Open 'Start Menu> All Programs> Microsoft Visual Studio 2010> Visual Studio Tools> Visual Studio 2010 Command Prompt'
- Trong cửa sổ lệnh, gõ:
xsd.exe /c /out:D:\ "D:\SampleSchema.xsd"
- tùy chọn xsd:
/out
tùy chọn để chỉ định thư mục đầu ra, /c
là để chỉ định công cụ để tạo các lớp học
- Hệ thống phân cấp lớp đầu ra có tại
D:\SampleSchema.cs
- Các tạo hệ thống cấp bậc lớp trông một số điều như thế này,
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.4952
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class note {
private string toField;
private string fromField;
private string headingField;
private string bodyField;
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string to {
get {
return this.toField;
}
set {
this.toField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string from {
get {
return this.fromField;
}
set {
this.fromField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string heading {
get {
return this.headingField;
}
set {
this.headingField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string body {
get {
return this.bodyField;
}
set {
this.bodyField = value;
}
}
}
Thêm lớp cho dự án visual studio.
Đối với mẫu xsd ở trên, lớp gốc là note
.
Gọi phương thức, thông tin
bool isXmlValid = ValidateXml(@"D:\Sample.xml",
@"D:\SampleSchema.xsd",
@"http://www.w3.org/2001/XMLSchema",
typeof(note));
thêm:
Nguồn
2011-01-03 11:58:33
Cảm ơn, Đó chính xác là những gì tôi cần! – M3NTA7
Thật đơn giản khi so sánh với các giải pháp khác ngoài kia, cảm ơn! – JBeagle
'validate' phải bằng chữ in hoa đầu tiên ... – realsonic