学习C#高级编程之XML

上一篇 / 下一篇  2008-01-22 14:45:54

验证XML文档的有效性---使用XMLReader

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Xml;

usingSystem.Xml.Schema;

 

namespaceXMLValidatingTest1

{

   publicpartialclassForm1:Form

   {

       publicForm1()

       {

           InitializeComponent();

       }

 

       privatevoidbutton1_Click(objectsender,EventArgse)

       {

           XmlReaderSettingssettings =newXmlReaderSettings();

           settings.Schemas.Add(null,@"D:\C#\books.xsd");

           settings.ValidationType = ValidationType.Schema;

//C#高级编程第四版中的例子是错误的,XsdValidate这个属性在.NET2.0中并不存在,这里//只需要使用ValidationType指定就可以

           settings.ValidationEventHandler +=newSystem.Xml.Schema.ValidationEventHandler(Settings_ValidationEventHandler);

           XmlReaderxr =XmlReader.Create(@"D:\C#\books.xml",settings);

           while(xr.Read())

           {

               if(xr.NodeType ==XmlNodeType.Text)

               {

                   this.listBox1.Items.Add(xr.Value);

               }

           }

       }

       privatevoidSettings_ValidationEventHandler(objectsender,ValidationEventArgsargs)

       {

           MessageBox.Show(args.Message);

       }

   }

}

 


TAG:

 

评分:0

我来说两句

Open Toolbar