Deserializing XML to Objects defined in multiple schemas - c#

I have an XML document containing types from 2 XML schemas. One (theirs.xsd) is a proprietary schema that I am integrating with (and cannot edit). To do this I am defining my own type (mine.xsd) that is an element within an 'any' element is the proprietary type.
I use Visual Studio's xsd.exe to generate C# classes from the schemas. However, the 'any' element in the proprietary type is generated as XmlElement[], and therefore my type doesn't get deserialized.
So I guess I can go one of two ways: either generate classes that will deserialize my type rather then keeping it as an XmlElement, or take the XmlElements and deserialize them individually. To deserialize I need an XmlReader, so I would need to go from an XmlElement to an XmlReader which I'm not sure how to do. Thanks.
Example:
File: theirs.xsd
<xs:element name="ProprietaryContainer">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
File: mine.xsd
<xs:element name="MyPairType">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type="xs:string"/>
<xs:element name="value" type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:element>
File: message.xml
<their:ProprietaryContainer>
<their:name>pairContainer</their:name>
<mine:MyPairType>
<mine:key>abc</mine:key>
<mine:value>long</mine:value>
</mine:MyPairType>
</their:ProprietaryContainer>

From the question:
To deserialize I need an XmlReader, so I would need to go from an XmlElement to an XmlReader which I'm not sure how to do
using(XmlReader reader = new XmlNodeReader(element)) {
//... use reader
}

Related

Retrieve default value for Optional Element in an XSD

I have the following XML:
<Person>
<Name>James</Name>
<Age>18</Age>
</Person>
In the XSD that validates this XML, I want to add another element. I want this element to be optional but with a default value - so if it is missing when the XML is validated, it is populated with the default value. Let's say the new element is "NumberOfSiblings" which defaults to 0. I've done this like so:
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Age" type="xs:integer" />
<xs:element name="NumberOfSiblings" type="xs:integer" minOccurs="0" default="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
So now let's assume I come to parse the XML above in .Net / C#. Is there a way to validate it so it would automatically create the element with the default value, and therefore the parser would not fall over? If not, what's the correct way to obtain this default value from the XSD?

Include in an xsd schema complex type defined in a different assembly

I need to use a complex type defined in a different assembly in my xsd schema. Both of my .xsd schemas are defined as embedded resources and I've tried to link the sheet I have to import in the assembly, who needs it with no results.
Basically, when I need to validate one of my xml pages, I call this function, but it isn't able to add in cascade the xml schema sets of the types inside Operations.
public static XmlSchema GetDocumentSchema(this Document doc)
{
var actualType = doc.GetType();
var stream = actualType.Assembly.GetManifestResourceStream(actualType.FullName);
if (stream == null)
{
throw new FileNotFoundException("Unable to load the embedded file [" + actualType.FullName + "]");
}
var documentSchema = XmlSchema.Read(stream, null);
foreach (XmlSchemaExternal xmlInclude in documentSchema.Includes)
{
var includeStream = xmlInclude.SchemaLocation != "Operations.xsd"
? actualType.Assembly.GetManifestResourceStream(xmlInclude.Id)
: typeof (Operations).Assembly.GetManifestResourceStream(xmlInclude.Id);
if (includeStream == null)
{
throw new FileNotFoundException("Unable to load the embedded include file [" + xmlInclude.Id + "]");
}
xmlInclude.Schema = XmlSchema.Read(includeStream, null);
}
return documentSchema;
}
This is the main schema:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ExampleSheet"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include id="Operations" schemaLocation="Operations.xsd"/>
<xs:element name="ExampleSheet">
<xs:complexType>
<xs:sequence>
<xs:element name="Operations" type="Operations"/>
</xs:sequence>
<xs:attribute name="version" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
And this is the schema of Operations:
<xs:schema id="Operations"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Operations" type="Operations"/>
<xs:complexType name="Operations">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Insert" type="Insert"/>
<xs:element name="InsertUpdate" type="InsertUpdate"/>
<xs:element name="Update" type="Update"/>
<xs:element name="Delete" type="Delete"/>
</xs:choice>
<xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="store" type="xs:string" use="required"/>
<xs:attribute name="chain" type="xs:string" use="optional"/>
</xs:complexType>
</xs:schema>
For example, if I have an ExampleSheet with an Insert, it isn't able to recognize it. Operations and Insert are classes who implement IXmlSerializable, and the first one retrieves the schema sets of the inner types using a custom XmlSchemaProvider.
Am I doing something wrong?
How can I help my ExampleSheet to accet the members of Operations?
Should it ExampleSheet implement IXmlSerializable so I can build the reader and writer as I want, and would the schema be still useful?
Instead of XmlSchema, have you looked into the XmlSchemaSet class?
I have not done a lot with XML Serialization, so I don't know if it will fit your current application, but I've used it before in a similar situation where I have to refer to types defined in 3 separate schemas.
The complete XmlSchemaSet object will then have access to all of the types in each of the schemas.

XSD Gen Classes That Reference a Common Type

I am using XSD's to define my DTO types in C#. I am using XSD.exe to gen the classes from the XSD's.
I have a Common.xsd that defines an Address type and I want to use this in more than one class:
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="Street1" type="xs:string"/>
<xs:element name="Street2" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="State" type="xs:string"/>
<xs:element name="Zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Address" type="mhm:Address"/>
I am referencing this in a company XSD:
<xs:include schemaLocation=".\Common.xsd"/>
<xs:complexType name="Company">
<xs:sequence>
<xs:element name="AdmCode" type="xs:string"/>
<xs:element name="CompanyCode" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
<xs:element ref="mhm:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Company" type="mhm:Company"/>
And an employee XSD:
<xs:include schemaLocation=".\Common.xsd"/>
<xs:complexType name="Employee">
<xs:sequence>
<xs:element name="EmployeeNumber" type="xs:int"/>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="Address" type="mhm:Address"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Employee" type="mhm:Employee"/>
I gen the classes using this command line:
xsd .\XSD\Common.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Employee.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
When I go to compile the project, I find that the Address type has been generated in both the Company.cs class file and the Employee.cs class file.
How can I get the Address type generated just once in the Common.cs class file and the Employee and Company types use this single Address type?
You can use XSD.exe with multiple file arguments:
xsd .\XSD\Common.xsd .\XSD\Employee.xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
You basically want to split out the common types into a common assembly which is references by your other types. You have two options:
Manually split them out. I know this is a generated file but if your source schemas are fairly static then it's a one off exercise.
Use svcutil.exe instead. However this is much more complicated and actually you may not even be able to do this unless your schemas all abide by certain guidelines. If you are interested see below for the process.
If you fancy option 2 above then here is the general process:
Extract the types from Common.xsd using the /dconly flag on svcutil. This will generate a class file with your common types.
Compile this class into an assembly
Extract the types from A.xsd using the /r flag and referencing your CommonTypes.dll assembly.
Do the same for B.xsd
However, this approach is based on svcutil using the DataContractSerializer to do the work, as the /r flag is not available to XmlSerializer. And this will only work if the your schemas adhere to the rather strict DCS rules (can be found here: http://msdn.microsoft.com/en-us/library/ms733112.aspx).
If these rules are not adhered to then svcutil will fall back to using XmlSerializer which does not support /r flag.

Deserialize an array of strings with DataContract

I have an xml schema with a type defined like this:
<xs:complexType name="InteractiveWorkflowAddress">
<xs:sequence>
<xs:element name="endpointAddresses" nillable="true" type="q1:ArrayOfstring" xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
<xs:element name="instanceId" type="ser:guid" />
</xs:sequence>
</xs:complexType>
where the ArrayOfstring type is defined as
<xs:complexType name="ArrayOfstring">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfstring" nillable="true" type="tns:ArrayOfstring" />
How can I deserialize the InteractiveWorkflowAddress type using the DataContract attribute? I tried the following
[DataContract(Namespace = Constants.Namespace)]
public class InteractiveWorkflowAddress {
[DataMember()]
public string[] EndpointAddresses;
[DataMember()]
public string InstanceId;
}
But while the InstanceId member is deserialized correctly, EndpointAddresses is always null.
The best way to figure out how something can be deserialized, is to first serialize. Serialize that InteractiveWorkflowAddress data structure and see the XML is creates. You might be able to List instead of string[] - and also you can control serialization with [XmlArray(...)] and [XmlArrayItem(...)] - which may help too.
The easist way is likely going to be doing some quick trial-and-error, by: making a change, serializing the class, seeing the output - then repeat. Hope that helps!

How to specify a child element in XML schema with a name but any content?

I am trying to write some XML schema code to specify that a particular element 'abc' may have a child element with name 'xyz', and that element may have any attributes, and any child elements.
At the moment I have this:
<xs:element name="abc">
<xs:complexType>
<xs:sequence>
<xs:element name="xyz">
<xs:complexType>
<xs:sequence>
<xs:any/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
But when I validate my XML against the schema, I get validation failures complaining about the child elements of the xyz element.
Your xs:any doesn't have any information attached to it, so it is looking for schema defined elements. If you want to be slack in your interpretation of the sub-elements, try this:
<xs:element name="abc">
<xs:complexType>
<xs:sequence>
<xs:element name="xyz">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax" />
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
This should get you past validation. If you have any expectation on the well-formedness of xyz's content, you can include a namespace using the namespace attribute for xs:any, and pull in another schema for that information.
Good luck, and I hope this helps!

Categories

Resources