Question xsd to xml - use serialization or not?

Liss

New member
Joined
Aug 16, 2011
Messages
1
Programming Experience
Beginner
I’m developing an application where I use wcf services . I use the web service the organization offer to:
  1. Send an xml schema populated with data
  2. Get a list of available web services
  3. Get a specific xsd schema
What I have to do is:
  1. Get a xsd schema form the authority and use this xsd to generate a xml populated with data
  2. Call the webservice where I can specify a number for a specific xsd schema , and get the xsd schema in return. Then I can validat my xml schema against it before I send it to the authority
  3. Use a web service where my xml string is in a string property of the wcf service and I send the xml file to the authority
My xsd looks (almost) like this:

VB.NET:
<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:br="http://www.br.se/or" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!--title='Tittle' lang='E'-->
    <xs:element name="Schema">
        <xs:annotation>
            <xs:documentation>
                <br:txt br:lang="E" br:txttype="LEDE">
                    <p>Some text</p>
                </br:txt>
            </xs:documentation>
            <xs:documentation>
 
 
…………………………
 
 
<xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" ref="Information"/>
                <xs:element minOccurs="0" ref="Payer"/>
            </xs:sequence>
            <xs:attribute fixed="105" name="numberr" type="xs:integer" use="required"/>
            <xs:attribute fixed="10360" name="externalnumberr" type="xs:integer" use="required"/>
                        <xs:attribute fixed="Title" name="title" type="xs:string" use="optional"/>
            <xs:attribute fixed="4895" name="groupid" type="xs:positiveInteger" use="optional"/>
            <xs:attribute name="id" use="optional">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="761065">
                            <xs:annotation>
                                <xs:documentation>Text</xs:documentation>
                            </xs:annotation>
                        </xs:enumeration>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:anyAttribute/>
        </xs:complexType>
    </xs:element>
<xs:element name="Info">
        <xs:annotation>
            <xs:documentation>
                <br:txt br:lang="E" br:txttype="LEDE">
                    <p>Some Text</p>
                </br:txt>
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Task">
                    <xs:annotation>
                        <xs:documentation>
                            <br:txt br:lang="E" br:txttype="LEDE">
                                <p>Some text</p>
                            </br:txt>
                        </xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element ref="Year">
                    <xs:annotation>
                        <xs:documentation>
                            <br:txt br:lang="E" br:txttype="LEDE">
                                <p>Some Text</p>
                            </br:txt>
                        </xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="Taskowner"/>
            </xs:sequence>
            <xs:attribute fixed="4877" name="id" type="xs:positiveInteger" use="required"/>
        </xs:complexType>
    </xs:element>
<xs:element name="TaskSpesialTask">
        <xs:annotation>
            <xs:documentation>
                <br:info br:type="some txt">1</br:info>
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="List1">
                    <xs:attribute fixed="212" name="schemaid" type="xs:positiveInteger" use="required"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="List2">
        <xs:annotation>
            <xs:documentation>
                <br:info br:type="id">2</br:info>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:length value="1"/>
            <xs:enumeration value="1">
                <xs:annotation>
                    <xs:documentation>
                        <br:txt br:lang="E" br:txttype="LEDE">
                            <p>Some text</p>
                        </br:txt>
                    </xs:documentation>
                </xs:annotation>
            </xs:enumeration>
            <xs:enumeration value="2">
                <xs:annotation>
                    <xs:documentation>
                        <br:txt br:lang="E" br:txttype="LEDE">
                            <p>Some text</p>
                        </br:txt>
                    </xs:documentation>
                </xs:annotation>
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
<xs:element name="Year">
        <xs:annotation>
            <xs:documentation>
                <br:info br:type="pl">2</br:info>
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="Format">
                    <xs:attribute fixed="692" name="schemaid" type="xs:positiveInteger" use="required"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="Format">
        <xs:annotation>
            <xs:documentation>
                <br:info br:type="id">6</br:info>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:gYear"/>
    </xs:simpleType>
<xs:element name="TaskOwner">
        <xs:annotation>
            <xs:documentation>
                <br:txt br:lang="E" br:txttype="LEDE">
                    <p>Some Text</p>
                </br:txt>
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="TaskoOwnerNumber">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="TaskOwnerName">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="TaskOwnerAdress
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="TaskOwnerPhone">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="TaskOwnerEmail">
                    <xs:annotation>   
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
            <xs:attribute fixed="48" name="id" type="xs:positiveInteger" use="required"/>
        </xs:complexType>
    </xs:element>
<xs:element name="TaskOwnerNumber">
        <xs:annotation>
        </xs:annotation>
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="TextFormat">
                    <xs:attribute fixed="21" name="id" type="xs:positiveInteger" use="required"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="TextFormat">
        <xs:annotation>
            <xs:documentation>
                <br:info br:type="id">1</br:info>
            </xs:documentation>
            <xs:documentation>
                <br:txt br:lang="E" br:txttype="F">
                    <p>Wrong number</p>
                </brreg:tekst>
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:length value="9"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="TaskOwner name">
        <xs:annotation>  
        </xs:annotation>
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="TextFormat">
                    <xs:attribute fixed="2" name="id" type="xs:positiveInteger" use="required"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:element name="TaskOwner Adress">
        <xs:annotation>
        </xs:annotation>
    </xs:element>
 
And so on for phone and email
 
 <xs:element name="Payers">
        <xs:annotation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="20000" minOccurs="0" ref="PayersInfo"/>
                <xs:element minOccurs="0" ref="PaymentTotal">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="NumberOfPayers">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
            <xs:attribute fixed="410" name="id" type="xs:positiveInteger" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="PayersInfo">
        <xs:annotation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" ref="PayerBirthNumber">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerNumber">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerBirthDay">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerName">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerAdress">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerPhone">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerEmail">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
                <xs:element minOccurs="0" ref="PayerSum">
                    <xs:annotation>
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
            <xs:attribute fixed="48" name="id" type="xs:positiveInteger" use="required"/>
        </xs:complexType>
    </xs:element>
   <xs:element name="PayerBirthNumber">
        <xs:annotation>
        </xs:annotation>
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="TextFormat">
                    <xs:attribute fixed="2305" name="id" type="xs:positiveInteger" use="required"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="TextFormat">
        <xs:annotation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:length value="11"/>
        </xs:restriction>
    </xs:simpleType>
 
 
And som on for PayerNumber, PayerBirthDay, PAYerNAMe, PayerAdress, PayerPhone, PayerEmail…..
 
</xs:schema>

The elements in the xsd are structured as this:

Schema
Info​
Year​
TaskOwner​
TaskOwnerName​
TaskOwnerAdress​
TaskOwnerPhone​
TaskOwnerEmail​
TaskOwnerNumber​
Task​
TaskSpecialTask​
TaskOrdinaryTask​
Payers​
PaymentTotal​
NumberOfPayers​
PayersInfo (list of payers)​
PayerBirtNumber​
PayerBirthDay​
PayerName​
PayerAdress​
PayerPhone​
PayerEmail​


(Pleas ignore if something is left out in the xsd - it is there just so I can explain my problem better)

My question is, how should I generate the xml based on the xsd? Shoud I deserialize the xsd (with xsd.exe) so I get a class, create object of that class and populate it with data, and then serialize the object back to xml or should I keeping everything in the form of an in-memory XML document like this:

VB.NET:
Imports System.Xml
Imports <xmlns:xsi=http://..........>
 
Dim xmlDocument = 
                <Schema xsi:noSchemaNamespaceLocation=./relativePathTo/MySchema.xsd”>
                <Info>
                               ……. …..

The xsd schema change once a year, but mostly small changes.

What are the best approach for me, and why?

Thanks in advance!
 
Last edited:
Back
Top