Validating an XML against referenced XSD in C#

You need to create an XmlReaderSettings instance and pass that to your XmlReader when you create it. Then you can subscribe to the ValidationEventHandler in the settings to receive validation errors. Your code will end up looking like this: using System.Xml; using System.Xml.Schema; using System.IO; public class ValidXSD { public static void Main() { // … Read more

How to make type depend on attribute value using Conditional Type Assignment

You can do this using XSD 1.1’s Conditional Type Assignment: <?xml version=”1.0″ encoding=”UTF-8″?> <xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:vc=”http://www.w3.org/2007/XMLSchema-versioning” elementFormDefault=”qualified” vc:minVersion=”1.1″> <xs:element name=”listOfA”> <xs:complexType> <xs:sequence> <xs:element name=”a” maxOccurs=”unbounded”> <xs:alternative test=”@type = 1″ type=”a1Type”/> <xs:alternative test=”@type = 2″ type=”a2Type”/> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name=”a1Type”> <xs:sequence> <xs:element name=”name”/> <xs:element name=”surname”/> </xs:sequence> </xs:complexType> <xs:complexType name=”a2Type”> <xs:sequence> <xs:element name=”name”/> <xs:element name=”id”/> … Read more

How to validate an XML file against an XSD file?

The Java runtime library supports validation. Last time I checked this was the Apache Xerces parser under the covers. You should probably use a javax.xml.validation.Validator. import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.*; import java.net.URL; import org.xml.sax.SAXException; //import java.io.File; // if you use File import java.io.IOException; … URL schemaFile = new URL(“http://host:port/filename.xsd”); // webapp example … Read more

How to reference a local XML Schema file correctly?

Add one more slash after file:// in the value of xsi:schemaLocation. (You have two; you need three. Think protocol://host/path where protocol is ‘file’ and host is empty here, yielding three slashes in a row.) You can also eliminate the double slashes along the path. I believe that the double slashes help with file systems that … Read more

Generate C# class from XML

If you are working on .NET 4.5 project in VS 2012 (or newer), you can just Special Paste your XML file as classes. Copy your XML file’s content to clipboard In editor, select place where you want your classes to be pasted From the menu, select EDIT > Paste Special > Paste XML As Classes

How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

How to link an XSD to an XML document depends upon whether the XML document is using namespaces or not… Without namespaces Use xsi:noNamespaceSchemaLocation to provide a hint as to the XSD to be used: XML <root xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”example.xsd”> <!– … –> </root> XSD <xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”> <xsd:element name=”root”> <!– … –> </xsd:element> </xsd:schema> With namespaces … Read more

XML attribute vs XML element

I use this rule of thumb: An Attribute is something that is self-contained, i.e., a color, an ID, a name. An Element is something that does or could have attributes of its own or contain other elements. So yours is close. I would have done something like: EDIT: Updated the original example based on feedback … Read more