Successfully Validating an XML Element

image_pdfimage_print
   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;
using System.IO;
public class MainClass {
    public static void Main() {
        string schema =
          @"<?xml version=&#039;1.0&#039; encoding=&#039;utf-8&#039;?>
    <xs:schema attributeFormDefault=&#039;unqualified&#039; elementFormDefault=&#039;qualified&#039;
         xmlns:xs=&#039;http://www.w3.org/2001/XMLSchema&#039;>
      <xs:element name=&#039;Books&#039;>
       <xs:complexType>
        <xs:sequence>
         <xs:element maxOccurs=&#039;unbounded&#039; name=&#039;Book&#039;>
          <xs:complexType>
           <xs:sequence>
            <xs:element name=&#039;FirstName&#039; type=&#039;xs:string&#039; />
           <xs:element minOccurs=&#039;0&#039; name=&#039;MiddleInitial&#039;
                type=&#039;xs:string&#039; />
              <xs:element name=&#039;LastName&#039; type=&#039;xs:string&#039; />
            </xs:sequence>
            <xs:attribute name=&#039;type&#039; type=&#039;xs:string&#039; use=&#039;required&#039; />
          </xs:complexType>
        </xs:element>
       </xs:sequence>
      </xs:complexType>
     </xs:element>
    </xs:schema>";

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

    }
}

    


This entry was posted in XML LINQ. Bookmark the permalink.