CSharp - Validate a single element

Description

Validate a single element

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;/*from  w w  w .j  a v a 2 s  .  co  m*/

class Program
{
    static void Main(string[] args)
    {
        string schema =
      @"<?xml version='1.0' encoding='utf-8'?>
          <xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified'
            xmlns:xs='http://www.w3.org/2001/XMLSchema'>
            <xs:element name='Books'>
              <xs:complexType>
                <xs:sequence>
                  <xs:element maxOccurs='unbounded' name='Book'>
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name='FirstName' type='xs:string' />
                        <xs:element minOccurs='0' name='MiddleInitial'
                          type='xs:string' />
                        <xs:element name='LastName' type='xs:string' />
                      </xs:sequence>
                      <xs:attribute name='type' type='xs:string' use='required' />
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:schema>";

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.Add("", XmlReader.Create(new StringReader(schema)));

        XDocument xDocument = new XDocument(
          new XElement("Books",
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "Joe"),
              new XElement("LastName", "Ruby")),
            new XElement("Book",
              new XAttribute("type", "Editor"),
              new XElement("FirstName", "PHP"),
              new XElement("LastName", "Python"))));

        Console.WriteLine("Here is the source XML document:");
        Console.WriteLine("{0}{1}{1}", xDocument, System.Environment.NewLine);

        //Now we will validate the document:

        bool valid = true;
        xDocument.Validate(schemaSet, (o, vea) =>
        {
            Console.WriteLine("An exception occurred processing object type {0}.",
              o.GetType().Name);

            Console.WriteLine(vea.Message);

            valid = false;
        }, true);

        Console.WriteLine("Document validated {0}.{1}",
          valid ? "successfully" : "unsuccessfully",
          System.Environment.NewLine);

        XElement bookParticipant = xDocument.Descendants("Book").
          Where(e => ((string)e.Element("FirstName")).Equals("Joe")).First();

        bookParticipant.Element("FirstName").AddAfterSelf(new XElement("MiddleInitial", "C"));

        valid = true;
        bookParticipant.Validate(bookParticipant.GetSchemaInfo().SchemaElement, schemaSet,
          (o, vea) =>
          {
              Console.WriteLine("An exception occurred processing object type {0}.",
              o.GetType().Name);

              Console.WriteLine(vea.Message);

              valid = false;
          }, true);

        Console.WriteLine("Element validated {0}.{1}",
          valid ? "successfully" : "unsuccessfully",
          System.Environment.NewLine);
    }
}

Result

Related Topic