Schema Validation Warnings and Errors - c#

When I run the xsd tool to generate vb classes against:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="Security" type="SecurityType"/>
<xs:complexType name="SecurityType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="UsernameToken" type="UsernameToken"/>
</xs:sequence>
<xs:attribute name="mustUnderstand" type="xs:string"/>
<xs:anyAttribute/>
</xs:complexType>
<xs:complexType name="UsernameToken">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Username" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="Password" type="Password"/>
</xs:sequence>
<xs:attribute name="Id" type="xs:string"/>
</xs:complexType>
<xs:complexType name="Password">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Type" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
I get the following schema validation warnings:
Type Password is not declared
Type UsernameToken is not declared
Type SecurityType is not declared
I get the following Error:
The datatype 'SecurityType' is missing

I added xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" and it worked.

Related

XSD2Code - Create a List of Objects when generate a class

I've created this XSD schema and I want to use XSD2Code to generate code. The problem I'm facing happens when I try to have a list of objects. It cannot recognize the complex type.
XSD Example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="INVOICE01">
<xs:complexType>
<xs:sequence>
<xs:element name="Test" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="nome" minOccurs="0" type="xs:string" ></xs:element>
<xs:element name="nome2" minOccurs="0" type="xs:string" ></xs:element>
<xs:element name="henrique">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="henrique1" minOccurs="0"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If I don't use the maxOccurs="unbounded" it work fine.

Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context

I'm trying to validate this XML file
<session>
<mic id="1" posname="T1" x="0.0" y="0.0" z="0.0" />
</session>
using this XSD file
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="session">
<xs:complexType>
<xs:sequence>
<xs:element name="mic" type="micType" minOccurs="1" maxOccurs="4">
</xs:element>
<xs:complexType name="micType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:int" name="id"/>
<xs:attribute type="xs:string" name="posname"/>
<xs:attribute type="xs:float" name="x"/>
<xs:attribute type="xs:float" name="y"/>
<xs:attribute type="xs:float" name="z"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
but I get this error message:
XmlSchema error: Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context.
If I just have the attribute definition for the mic element the program runs fine. I don't know what I'm doing wrong. I'm trying to have the XSD validate the data types for the mic element. Can anybody please tell me what I'm doing wrong?
The complexType named micType should be defined as a global type declaration, i.e. it should be an immediate child of the xs:schema element.
What you have is a non-viable hybrid of a local and global declaration of a complex type. Either define a micType globally (as Michael Kay mentioned) or locally:
Global complexType declaration
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="session">
<xs:complexType>
<xs:sequence>
<xs:element name="mic" type="micType" minOccurs="1" maxOccurs="4"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="micType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:int" name="id"/>
<xs:attribute type="xs:string" name="posname"/>
<xs:attribute type="xs:float" name="x"/>
<xs:attribute type="xs:float" name="y"/>
<xs:attribute type="xs:float" name="z"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
Local complexType declaration
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="session">
<xs:complexType>
<xs:sequence>
<xs:element name="mic" minOccurs="1" maxOccurs="4">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:int" name="id"/>
<xs:attribute type="xs:string" name="posname"/>
<xs:attribute type="xs:float" name="x"/>
<xs:attribute type="xs:float" name="y"/>
<xs:attribute type="xs:float" name="z"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Either way is ok and will successfully validate your XML.

XML Schema with repeating elements

I am trying to deserialise the following XML:
<queries>
<query name='abc'>
<statement>SELECT * FROM table1</statement>
<label row='1' name='id'/>
<label row='2' name='name'/>
</query>
<query name='another'>
<statement>SELECT * FROM table2</statement>
<label row='1' name='myTabID'/>
<label row='2' name='myTabName'/>
</query>
</queries>
To validate the schema I am using:
<xs:complexType name="Queries">
<xs:sequence>
<xs:element name="query" type="Query"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Query">
<xs:sequence>
<xs:element name="statement" minOccurs="1" maxOccurs="1"/>
<xs:element name="label">
<xs:complexType>
<xs:attribute name="row" type="xs:int" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
Validation is failing with the message:
Not valid.
Error - Line 7, 48: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 48; cvc-complex-type.2.4.d: Invalid content was found starting with element 'label'. No child element is expected at this point.
Error - Line 9, 31: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 31; cvc-complex-type.2.4.d: Invalid content was found starting with element 'query'. No child element is expected at this point.
What am I missing please? Do I need to specify a different tag than sequence? I want to enforce at least one query existing.
Thank you in advanced :)
The error message complains about the definition
<xs:element name="label">
and the use
<label row='1' name='id'/>
<label row='2' name='name'/>
The default values (W3) for minOccurs and maxOccurs is 1, so you can only use one label there. You need to change it if you like to use more labels. Use unbounded for arbitrary amount.
Also note that similar maxOccurs adjustment (unbounded or greater than 1) is needed for query (as commented by #kjhughes)
try this :
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="queries">
<xs:complexType>
<xs:sequence>
<xs:element name="query" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="statement"/>
<xs:element name="label" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:int" name="row" use="optional"/>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

How to create child node using named types in XML

I have the below xsd schema and I want to add the DG collection inside DGItems. So I want to know how can I achieve that.
When look at my xml at the 2nd section in that manner I want to achieve that. Just a little help needed to fix that.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="InvoiceData" type="InvoiceData"/>
<xs:complexType name="InvoiceData">
<xs:sequence>
<xs:element name="HeaderFields" type="HeaderFields"/>
<xs:element name="DGItems" type="DGItems"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="HeaderFields">
<xs:sequence>
<xs:element name="CompanyId" type="xs:string" />
<xs:element name="ImageID" type="xs:string" />
<xs:element name="Incident" type="xs:string" />
<xs:element name="FacilityID" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="DGItems">
<xs:sequence>
<xs:element name="DG1" type="xs:string" />
<xs:element name="DG2" type="xs:string" />
<xs:element name="DG3" type="xs:string" />
<xs:element name="DG4" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
<InvoiceValues>
<HeaderFields>
<CompanyId>StringValue</CompanyId>
<ImageID>StringValue</ImageID>
<Incident>StringValue</Incident>
<FacilityID>StringValue</FacilityID>
</HeaderFields>
<DGItems>
<Dg>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
</Dg>
<Dg>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
<DG1>StringValue</DG1>
</Dg>
</DGItems>
</InvoiceValues>
Need your help on this.
I have solved the issue on my own, the thing which i have done to fix my issue is are as follows : I have added the Element AI inside the Sequence node & in this way I was managed to fix my issue.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmployeeData" type="EmployeeData"/>
<xs:complexType name="EmployeeData">
<xs:sequence>
<xs:element name="EmployeeHeaderData" type="EmployeeHeaderData"/>
<xs:element name="AddressItems" type="AddressItems"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="EmployeeHeaderData">
<xs:sequence>
<xs:element name="CompanyId" type="xs:string" />
<xs:element name="ImageID" type="xs:string" />
<xs:element name="Incident" type="xs:string" />
<xs:element name="FacilityID" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="AddressItems">
<xs:sequence>
<xs:element name="AI" minOccurs="0" maxOccurs="unbounded" >
<xs:complexType>
<xs:sequence>
<xs:element name="AI1" type="xs:string" />
<xs:element name="AI2" type="xs:string" />
<xs:element name="AI3" type="xs:string" />
<xs:element name="AI4" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

Problem deserializing xml file

I auto generated an xsd file from the below xml and used xsd2code to get a c# class. The problem is the entire xml doesn't deserialize.
Here is how I'm attempting to deserialize:
static void Main(string[] args)
{
using (TextReader textReader = new StreamReader("config.xml"))
{
// string temp = textReader.ReadToEnd();
XmlSerializer deserializer = new XmlSerializer(typeof(project));
project p = (project)deserializer.Deserialize(textReader);
}
}
here is the actual XML:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<scm class="hudson.scm.SubversionSCM">
<locations>
<hudson.scm.SubversionSCM_-ModuleLocation>
<remote>https://svn.xxx.com/test/Validation/CPS DRTest DLL/trunk</remote>
</hudson.scm.SubversionSCM_-ModuleLocation>
</locations>
<useUpdate>false</useUpdate>
<browser class="hudson.scm.browsers.FishEyeSVN">
<url>http://fisheye.xxxx.net/browse/Test/</url>
<rootModule>Test</rootModule>
</browser>
<excludedCommitMessages></excludedCommitMessages>
</scm>
<openf>Hello there</openf>
<buildWrappers/>
</project>
When I run the above, the locations node remains null.
Here is the xsd that I'm using:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="project">
<xs:complexType>
<xs:all>
<xs:element name="openf" type="xs:string" minOccurs="0" />
<xs:element name="buildWrappers" type="xs:string" minOccurs="0" />
<xs:element name="scm" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="useUpdate" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
<xs:element name="excludedCommitMessages" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
<xs:element name="locations" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="hudson.scm.SubversionSCM_-ModuleLocation" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="remote" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="browser" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="url" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
<xs:element name="rootModule" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
</xs:sequence>
<xs:attribute name="class" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="class" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="project" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Figured it out. When using xsd2code I had select options to target the 3.5 framework and include the xml attributes. It now deserializes as expected. Not sure which one did it, but works now.
According to this page '.' is not a valid XML tag name character, so you need to rename <hudson.scm.SubversionSCM_-ModuleLocation> to something without the dots.

Categories

Resources