Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Create Xml schema
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
public class MainClass
{
public static void Main()
{
XmlSchema schema = new XmlSchema();
XmlSchemaSimpleType nametype = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction nameRes = new XmlSchemaSimpleTypeRestriction();
nameRes.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMinLengthFacet nameFacet1 = new XmlSchemaMinLengthFacet();
nameFacet1.Value = "3";
XmlSchemaMaxLengthFacet nameFacet2 = new XmlSchemaMaxLengthFacet();
nameFacet2.Value = "255";
nameRes.Facets.Add(nameFacet1);
nameRes.Facets.Add(nameFacet2);
nametype.Content = nameRes;
XmlSchemaSimpleType phonetype = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction phoneRes = new XmlSchemaSimpleTypeRestriction();
phoneRes.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet phoneFacet1 = new XmlSchemaMaxLengthFacet();
phoneFacet1.Value = "20";
phoneRes.Facets.Add(phoneFacet1);
phonetype.Content = phoneRes;
XmlSchemaSimpleType notestype = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction notesRes = new XmlSchemaSimpleTypeRestriction();
notesRes.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet notesFacet1 = new XmlSchemaMaxLengthFacet();
notesFacet1.Value = "500";
notesRes.Facets.Add(notesFacet1);
notestype.Content = notesRes;
XmlSchemaComplexType employeetype = new XmlSchemaComplexType();
XmlSchemaSequence sequence = new XmlSchemaSequence();
XmlSchemaElement firstname = new XmlSchemaElement();
firstname.Name = "firstname";
firstname.SchemaType = nametype;
XmlSchemaElement homephone = new XmlSchemaElement();
homephone.Name = "homephone";
homephone.SchemaType = phonetype;
XmlSchemaElement notes = new XmlSchemaElement();
notes.Name = "notes";
notes.SchemaType = notestype;
sequence.Items.Add(firstname);
sequence.Items.Add(homephone);
sequence.Items.Add(notes);
employeetype.Particle = sequence;
XmlSchemaAttribute employeeid = new XmlSchemaAttribute();
employeeid.Name = "employeeid";
employeeid.SchemaTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
employeeid.Use = XmlSchemaUse.Required;
employeetype.Attributes.Add(employeeid);
XmlSchemaComplexType complextype = new XmlSchemaComplexType();
XmlSchemaSequence sq = new XmlSchemaSequence();
XmlSchemaElement employee = new XmlSchemaElement();
employee.Name = "employee";
employee.SchemaType = employeetype;
employee.MinOccurs = 0;
employee.MaxOccursString = "unbounded";
sq.Items.Add(employee);
complextype.Particle = sq;
XmlSchemaElement employees = new XmlSchemaElement();
employees.Name = "employees";
employees.SchemaType = complextype;
schema.Items.Add(employees);
XmlSchemaSet set = new XmlSchemaSet();
set.Add(schema);
set.rupile();
XmlTextWriter writer=new XmlTextWriter("yourvalue",null);
schema.Write(writer);
writer.Close();
}
}
Register Schema validation event
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
XmlDocument items = new XmlDocument();
items.Load("items.xml");
XmlSchema schema = XmlSchema.Read(new FileStream("items.xsd",FileMode.Open), new ValidationEventHandler(OnSchemaValidate));
items.Schemas.Add(schema);
items.Validate(new ValidationEventHandler(OnValidate));
}
static void OnValidate(object sender, ValidationEventArgs vargs)
{
Console.WriteLine(vargs.Message);
}
static void OnSchemaValidate(object sender, ValidationEventArgs vargs)
{
Console.WriteLine(vargs.Message);
}
}
Schema Validation
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
XmlDocument items = new XmlDocument();
items.Load("items.xml");
XmlSchema schema = XmlSchema.Read(new FileStream("items.xsd",FileMode.Open), new ValidationEventHandler(OnSchemaValidate));
items.Schemas.Add(schema);
items.Validate(new ValidationEventHandler(OnValidate));
}
static void OnValidate(object sender, ValidationEventArgs vargs)
{
Console.WriteLine(vargs.Message);
}
static void OnSchemaValidate(object sender, ValidationEventArgs vargs)
{
Console.WriteLine(vargs.Message);
}
}