1
這個代碼不驗證XML正確能否請您發現錯誤一個Xml ....即使我與無效的XML執行它不產生任何錯誤如何驗證使用XSD
using System.Xml;
namespace XmlTryProject
{
internal class Program
{
private static void Main(string[] args)
{
XmlReaderSettings readSettings = new XmlReaderSettings();
readSettings.ValidationType = ValidationType.Schema;
readSettings.Schemas.Add(null,
@"C:\Visual Studio 2010\Projects\XmlTry \XmlTryProject\EmployeeXSD.xsd");
readSettings.ValidationEventHandler +=
new System.Xml.Schema.ValidationEventHandler(Validater);
XmlReader xReader = XmlReader.Create(
@"C:\Visual Studio 2010\Projects\XmlTry\XmlTryProject\EmployeeXML.xml",
readSettings);
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
Console.WriteLine(xReader.Name);
}
}
}
public static void Validater(object sender,
System.Xml.Schema.ValidationEventArgs args)
{
Console.WriteLine(args.Message);
}
}
}
我懷疑計算器問題將有所幫助:http://stackoverflow.com/questions/751511/validating-an-xml-against-referenced-xsd-in-c-sharp –