xsi:type attribute messing up C# XML deserialization

The short answer is that you need to manually add [XmlInclude(typeof(SequencePoint))] to your MethodPoint class: [System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “4.0.30319.33440”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] [XmlInclude(typeof(SequencePoint))] public partial class CoverageSessionModulesModuleClassesClassMethodsMethodMethodPoint { private string vcField; private string uspidField; private string ordinalField; private string offsetField; private string slField; private string scField; private string elField; private string ecField; private string becField; private … Read more

Generating XML file using XSD file

Suppose we have Test.xsd file that looks like this: <?xml version=”1.0″?> <xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”> <xs:element name=”MyClass”> <xs:complexType> <xs:sequence> <xs:element name=”Field1″ type=”xs:string”/> <xs:element name=”Field2″ type=”xs:string”/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Create classes using xsd tool: xsd.exe /classes Test.xsd This will generate Test.cs file. Add Test.cs file to your solution. Create instance of MyClass, defined in XSD schema and … Read more

XML Schema that allows anything (xsd:any)

XML Schema cannot specify that a document is valid regardless of its content. However, if you’re able to specify the root element, you can use xs:anyAttribute and xs:any to allow any attributes on the root element and any XML under the root: <?xml version=”1.0″ encoding=”utf-8″?> <xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”> <xs:element name=”root”> <xs:complexType> <xs:sequence> <xs:any processContents=”skip” minOccurs=”0″ maxOccurs=”unbounded”/> … Read more

Does XML care about the order of elements?

XML schema compositor “sequence” will enforce ordering I know this is old but I just came upon the post. Until today I would most likely answer the question Does XML care about the order of elements? with No, unless you use a poorly written xml parser. However, today a third party application complained that the … Read more

cvc-elt.1: Cannot find the declaration of element ‘MyElement’

Your schema is for its target namespace http://www.example.org/Test so it defines an element with name MyElement in that target namespace http://www.example.org/Test. Your instance document however has an element with name MyElement in no namespace. That is why the validating parser tells you it can’t find a declaration for that element, you haven’t provided a schema … Read more

Validating XML against XSD [duplicate]

Returns simply true or false (also you don’t need any external library): static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch(Exception ex) { return false; } }