XML之Validating
来源:互联网 发布:边际报酬递减规律算法 编辑:程序博客网 时间:2024/05/19 22:00
using System;
using System.Xml;
using System.Xml.Schema;
class XmlDocumentValidationExample
{
static void Main(string[] args)
{
try
{
// Create a new XmlDocument instance and load
// the XML document into the DOM.
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
// Add the XML schema for the XML document to the
// Schemas property of the XmlDocument.
document.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
// Validate the XML document loaded into the DOM.
document.Validate(ValidationEventHandler);
// Make an invalid change to the first and last
// price elements in the XML document, and write
// the XmlSchemaInfo values assigned to the price
// element during validation to the console.
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");
XmlNode priceNode = document.SelectSingleNode(@"/bk:bookstore/bk:book/bk:price", manager);
Console.WriteLine("SchemaInfo.IsDefault: {0}", priceNode.SchemaInfo.IsDefault);
Console.WriteLine("SchemaInfo.IsNil: {0}", priceNode.SchemaInfo.IsNil);
Console.WriteLine("SchemaInfo.SchemaElement: {0}", priceNode.SchemaInfo.SchemaElement);
Console.WriteLine("SchemaInfo.SchemaType: {0}", priceNode.SchemaInfo.SchemaType);
Console.WriteLine("SchemaInfo.Validity: {0}", priceNode.SchemaInfo.Validity);
priceNode.InnerXml = "A";
XmlNodeList priceNodes = document.SelectNodes(@"/bk:bookstore/bk:book/bk:price", manager);
XmlNode lastprice = priceNodes[priceNodes.Count - 1];
lastprice.InnerXml = "B";
// Validate the XML document with the invalid changes.
// The invalid changes cause schema validation errors.
document.Validate(ValidationEventHandler);
// Correct the invalid change to the first price element.
priceNode.InnerXml = "8.99";
// Validate only the first book element. The last book
// element is invalid, but not included in validation.
XmlNode bookNode = document.SelectSingleNode(@"/bk:bookstore/bk:book", manager);
document.Validate(ValidationEventHandler, bookNode);
}
catch (XmlException ex)
{
Console.WriteLine("XmlDocumentValidationExample.XmlException: {0}", ex.Message);
}
catch(XmlSchemaValidationException ex)
{
Console.WriteLine("XmlDocumentValidationExample.XmlSchemaValidationException: {0}", ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("XmlDocumentValidationExample.Exception: {0}", ex.Message);
}
}
static void ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.Write("\nWARNING: ");
else if (args.Severity == XmlSeverityType.Error)
Console.Write("\nERROR: ");
Console.WriteLine(args.Message);
}
}
//注意xmls的验证
- XML之Validating
- PHP - XML - Validating XML
- validating
- C#学习之winform控件中validating和validated区别
- Validating Expressions
- validating问题
- 去掉Eclipse中的Validating
- Validating Checkboxlist using CustomValidator
- 去掉Eclipse中的Validating
- 去掉Eclipse中的Validating
- Validating Receipts Locally
- MyEclipse10 不停的 Validating...
- eclipse validating 很慢
- eclipse validating 很慢
- eclipse validating 很慢
- 如何将validating除去
- MyEclipse10 不停的 Validating...
- validating & update ctabfolder css
- jetty中文文档
- linux c/c++ 解决隐式声明与内建函数不兼容问题
- Myeclipse性能优化(综合)
- tomcat连接池
- linux 下获取root权限
- XML之Validating
- 局域网内不能访问SQL Server 2005 的解决办法
- RedHat5.4上iscsi+multipath多路径存储配置
- Java里timer执行定时任务
- 常用cmd命令
- 使用Axis编写WebService
- 除了技术我们还应该知道的
- 画正弦线
- Java I/O