Csharp/C Sharp by API/System.Xml.Schema/XmlSchema

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

XmlSchema.Compile

  
using System;
using System.IO;
using System.Xml.Schema;
public class ValidateSchema {
  public static void Main(string [] args) {
    ValidationEventHandler handler = new ValidationEventHandler(ValidateSchema.Handler);
    XmlSchema schema = XmlSchema.Read(File.OpenRead(args[0]),handler);
    schema.rupile(handler);
  }
  public static void Handler(object sender, ValidationEventArgs e) {
    Console.WriteLine(e.Message);
  }
}


XmlSchema.Write

  
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XDocument xDocument = new XDocument(
          new XElement("Books",
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "A"),
              new XElement("LastName", "B")),
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "C"),
              new XElement("LastName", "D"))));
        Console.WriteLine(xDocument);
        xDocument.Save("bookparticipants.xml");
        XmlSchemaInference infer = new XmlSchemaInference();
        XmlSchemaSet schemaSet = infer.InferSchema(new XmlTextReader("bookparticipants.xml"));
        XmlWriter w = XmlWriter.Create("bookparticipants.xsd");
        foreach (XmlSchema schema in schemaSet.Schemas()) {
            schema.Write(w);
        }
        w.Close();
        XDocument newDocument = XDocument.Load("bookparticipants.xsd");
        Console.WriteLine(newDocument);
    }
}