public abstract class XmlSchemaObject
|
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Reflection;
public class ValidXSD {
public static int Main() {
string xsd = "example.xsd";
FileStream fs;
XmlSchema schema;
try {
fs = new FileStream(xsd, FileMode.Open);
schema = XmlSchema.Read(fs, new ValidationEventHandler(ShowCompileError));
schema.Compile(new ValidationEventHandler(ShowCompileError));
if (schema.IsCompiled) {
DisplayObjects(schema);
}
return 0;
} catch (XmlSchemaException e) {
Console.WriteLine("LineNumber = {0}", e.LineNumber);
Console.WriteLine("LinePosition = {0}", e.LinePosition);
Console.WriteLine("Message = {0}", e.Message);
Console.WriteLine("Source = {0}", e.Source);
return -1;
}
catch (Exception e) {
Console.WriteLine(e);
return -1;
}
}
public static void DisplayObjects(object o) {
DisplayObjects(o, "");
}
public static void DisplayObjects(object o, string indent) {
Console.WriteLine("{0}{1}", indent, o);
foreach (PropertyInfo property in o.GetType().GetProperties()) {
if (property.PropertyType.FullName == "System.Xml.Schema.XmlSchemaObjectCollection") {
XmlSchemaObjectCollection childObjectCollection = (XmlSchemaObjectCollection) property.GetValue(o, null);
foreach (XmlSchemaObject schemaObject in childObjectCollection) {
DisplayObjects(schemaObject, indent + "\t");
}
}
}
}
public static void ShowCompileError(object sender, ValidationEventArgs e) {
Console.WriteLine("Validation Error: {0}", e.Message);
}
}
The example uses the example.xsd file as input.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="testGroup">
<xs:choice>
<xs:any namespace="##any"/>
</xs:choice>
</xs:group>
<xs:element name="myElement" >
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:group ref="testGroup" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
| LineNumber | Read-write Gets or sets the line number in the file to which the schema element refers. |
| LinePosition | Read-write Gets or sets the line position in the file to which the schema element refers. |
| Namespaces | Read-write |
| SourceUri | Read-write Gets or sets the source location for the file that loaded the schema. |
| Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
| GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
| GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
| ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
| ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. |
| Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
| MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
protected XmlSchemaObject(); |
public int LineNumber {get; set;}
|
public int LinePosition {get; set;}
|
public XmlSerializerNamespaces Namespaces {get; set;}
|
public string SourceUri {get; set;}
|
~XmlSchemaObject(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |