Created
February 25, 2016 23:37
-
-
Save robertmuehsig/9f9d8aae83c751b24c61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml; | |
using System.Xml.Linq; | |
using System.Xml.Schema; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string xsdMarkup = | |
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> | |
<xsd:element name='Root'> | |
<xsd:complexType> | |
<xsd:sequence> | |
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/> | |
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'> | |
<xsd:complexType> | |
<xsd:simpleContent> | |
<xsd:extension base='xsd:string'> | |
<xsd:attribute name='Att1' default='Att1 Default Value'/> | |
</xsd:extension> | |
</xsd:simpleContent> | |
</xsd:complexType> | |
</xsd:element> | |
</xsd:sequence> | |
</xsd:complexType> | |
</xsd:element> | |
</xsd:schema>"; | |
XmlSchemaSet schemas = new XmlSchemaSet(); | |
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup))); | |
XDocument doc1 = new XDocument( | |
new XElement("Root", | |
new XElement("Child1", "c1"), | |
new XElement("Child2", "c2") | |
) | |
); | |
XDocument doc2 = new XDocument( | |
new XElement("Root", | |
new XElement("Child1", "content1"), | |
new XElement("Child3", "content1") | |
) | |
); | |
Console.WriteLine("Validating doc1"); | |
bool errors = false; | |
doc1.Validate(schemas, (o, e) => | |
{ | |
Console.WriteLine("{0}", e.Message); | |
errors = true; | |
}, true); | |
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated"); | |
Console.WriteLine(); | |
Console.WriteLine("Validating doc2"); | |
errors = false; | |
doc2.Validate(schemas, (o, e) => | |
{ | |
Console.WriteLine("{0}", e.Message); | |
errors = true; | |
}, true); | |
Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated"); | |
Console.WriteLine(); | |
Console.WriteLine("Contents of doc1:"); | |
Console.WriteLine(doc1); | |
Console.WriteLine(); | |
Console.WriteLine("Contents of doc2:"); | |
Console.WriteLine(doc2); | |
var test = doc2.Elements().First().GetSchemaInfo(); | |
var element = test.SchemaElement; | |
// Iterate over each XmlSchemaElement in the Values collection | |
// of the Elements property. | |
Console.WriteLine("Selecting Element: {0}", element.Name); | |
// Get the complex type of the Customer element. | |
XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType; | |
// If the complex type has any attributes, get an enumerator | |
// and write each attribute name to the console. | |
if (complexType.AttributeUses.Count > 0) | |
{ | |
IDictionaryEnumerator enumerator = | |
complexType.AttributeUses.GetEnumerator(); | |
while (enumerator.MoveNext()) | |
{ | |
XmlSchemaAttribute attribute = | |
(XmlSchemaAttribute)enumerator.Value; | |
Console.WriteLine("Attribute: {0}", attribute.Name); | |
} | |
} | |
// Get the sequence particle of the complex type. | |
XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence; | |
// Iterate over each XmlSchemaElement in the Items collection. | |
foreach (XmlSchemaObject childElement in sequence.Items) | |
{ | |
var asElement = (XmlSchemaElement)childElement; | |
Console.WriteLine("Element: {0}", asElement.Name); | |
} | |
} | |
//XDocument doc = XDocument.Load("Sample.xml"); | |
//XmlSchemaSet schemaSet = new XmlSchemaSet(); | |
//schemaSet.Add("", XmlReader.Create(new StringReader(File.ReadAllText("Schema.xsd")))); | |
//XmlReaderSettings xrs = new XmlReaderSettings(); | |
//xrs.ValidationType = ValidationType.Schema; | |
//xrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; | |
//xrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; | |
//xrs.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; | |
//xrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; | |
//xrs.Schemas = schemaSet; | |
//List<XElement> errorElements = new List<XElement>(); | |
//xrs.ValidationEventHandler += (o, s) => { | |
// Console.WriteLine("{0}: {1}", s.Severity, s.Message); | |
// var exception = (s.Exception as XmlSchemaValidationException); | |
// if (exception != null) | |
// { | |
// var element = (exception.SourceObject as XElement); | |
// if (element != null) | |
// errorElements.Add(element); | |
// } | |
//}; | |
//using (XmlReader xr = XmlReader.Create(doc.CreateReader(), xrs)) | |
//{ | |
// while (xr.Read()) { } | |
//} | |
//foreach(var foo in errorElements) | |
//{ | |
// var si = foo.GetSchemaInfo(); | |
// // do something with SchemaInfo | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment