By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,987 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,987 IT Pros & Developers. It's quick & easy.

creating xml document using schema

P: n/a
I have a task to create xml document from c# code.

I have example of that document should look like and also a xml schema.
I wonder if i can use this xml schema somehow ?

This is schema:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Draft">
<xs:complexType>
<xs:sequence>
<xs:element name="DraftId" type="xs:string" />
<xs:element name="StartDate" type="xs:dateTime" />
<xs:element name="RetryCount" type="xs:unsignedByte" />
<xs:element name="LocalCurrencyId" type="xs:unsignedByte" />
<xs:element name="EndDate" type="xs:dateTime" />
<xs:element name="OriginalPremium" type="xs:unsignedShort" />
<xs:element name="AuthorizationCode" type="xs:string" />
<xs:element name="Locale" type="xs:string" />
<xs:element name="IssueDate" type="xs:dateTime" />
<xs:element name="StatusCode" type="xs:string" />
<xs:element name="Invoice">
<xs:complexType>
<xs:sequence>
<xs:element name="Number" type="xs:string" />
<xs:element name="IssueDate" type="xs:dateTime" />
<xs:element name="DueDate" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="PolicyNumber" type="xs:unsignedLong" />
<xs:element name="PolicyHolderName" type="xs:string" />
<xs:element name="OriginalCurrencyId" type="xs:unsignedByte" />
<xs:element name="TransactionId" type="xs:string" />
<xs:element name="LocalPremium" type="xs:decimal" />
<xs:element name="PaymentType" type="xs:string" />
<xs:element name="MediatorName" type="xs:string" />
<xs:element name="LobId" type="xs:unsignedByte" />
<xs:element name="PolicyHolder">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Surname" type="xs:string" />
<xs:element name="Code" type="xs:string" />
<xs:element name="BirthDate" type="xs:dateTime" />
<xs:element name="IsPolicyHolder" type="xs:boolean" />
<xs:element name="IsNotResident" type="xs:boolean" />
<xs:element name="Address" type="xs:string" />
<xs:element name="Phone" type="xs:unsignedInt" />
<xs:element name="Email" type="xs:string" />
<xs:element name="Type" type="xs:string" />
<xs:element name="ResidencyCountry" type="xs:unsignedByte" />
<xs:element name="Sex" type="xs:string" />
<xs:element name="Village" type="xs:string" />
<xs:element name="House" type="xs:string" />
<xs:element name="Flat" type="xs:string" />
<xs:element name="TownDistrictOid" type="xs:unsignedByte" />
<xs:element name="RegionCityOid" type="xs:unsignedByte" />
<xs:element name="Street" type="xs:string" />
<xs:element name="PostalIndex" type="xs:unsignedShort" />
<xs:element name="Country" type="xs:unsignedByte" />
<xs:element name="LegalPersonType" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Country" type="xs:string" />
<xs:element name="TravelType" type="xs:unsignedByte" />
<xs:element name="Zone" type="xs:unsignedByte" />
<xs:element name="Program" type="xs:unsignedByte" />
<xs:element name="InsuredPersons">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Surname" type="xs:string" />
<xs:element name="Code" type="xs:string" />
<xs:element name="BirthDate" type="xs:dateTime" />
<xs:element name="IsPolicyHolder" type="xs:boolean" />
<xs:element name="IsNotResident" type="xs:boolean" />
<xs:element name="Type" type="xs:string" />
<xs:element name="ResidencyCountry"
type="xs:unsignedByte" />
<xs:element name="Sex" type="xs:string" />
<xs:element name="TownDistrictOid"
type="xs:unsignedByte" />
<xs:element name="RegionCityOid" type="xs:unsignedByte"
/>
<xs:element name="Country" type="xs:unsignedByte" />
<xs:element name="LegalPersonType"
type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

This is how final document should look like:

<?xml version="1.0" encoding="utf-16"?>
<Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="TravelDraft">
<DraftId>139451de-1bc1-4070-b9d1-548dd4a7f812</DraftId>
<StartDate>2006-10-04T00:00:00</StartDate>
<RetryCount>0</RetryCount>
<LocalCurrencyId>1</LocalCurrencyId>
<EndDate>2006-10-25T23:59:59</EndDate>
<OriginalPremium>38.61</OriginalPremium>
<Locale>lt-LT</Locale>
<IssueDate>2006-10-02T09:26:31.8562127+03:00</IssueDate>
<StatusCode>Unsuccessful</StatusCode>
<Invoice>
<Number>B0002624</Number>
<IssueDate>2006-10-02T09:26:31.8562127+03:00</IssueDate>
<DueDate>2006-10-02T09:26:31.8562127+03:00</DueDate>
</Invoice>
<PolicyNumber>45300000265</PolicyNumber>
<PolicyHolderName>AS AS, 123456-12345</PolicyHolderName>
<OriginalCurrencyId>3</OriginalCurrencyId>
<TransactionId>FVR21tY1Kot1fOfFRqkFJ9WZ7E4=</TransactionId>
<LocalPremium>27.03</LocalPremium>
<PaymentType>Ita</PaymentType>
<MediatorName>Air Baltic Corporation AS</MediatorName>
<LobId>53</LobId>
<PolicyHolder>
<Name>AS</Name>
<Surname>AS</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>false</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Address></Address>
<Phone>11212121</Phone>
<Email>AS**@ASAS.LV</Email>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<Village />
<House>NAMAS</House>
<Flat />
<TownDistrictOid>0</TownDistrictOid>
<RegionCityOid>27</RegionCityOid>
<Street />
<PostalIndex>1212</PostalIndex>
<LegalPersonType>0</LegalPersonType>
</PolicyHolder>
<Country>Estonia</Country>
<Zone>1</Zone>
<TravelType>21</TravelType>
<Program>1</Program>
<InsuredPersons>
<Person>
<Name>AS</Name>
<Surname>AS</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>true</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<LegalPersonType>0</LegalPersonType>
</Person>
<Person>
<Name>AS2</Name>
<Surname>AS2</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>true</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<LegalPersonType>0</LegalPersonType>
</Person>
</InsuredPersons>
</Draft>

This is my code so far (as you can see i'm not using schema, cuz i don't
know how):

XmlDocument xml = new XmlDocument();

XmlDeclaration declaration = xml.CreateXmlDeclaration ("1.0",
utf-16", null);

xml.AppendChild(declaration);

//Root element
XmlElement elemDraft = xml.CreateElement
("Draft");//,"http://www.w3.org/2001/XMLSchema");

//Root element attributes
XmlAttribute attr1 = xml.CreateAttribute ("xmlns:xsi");
attr1.Value = "http://www.w3.org/2001/XMLSchema-instance";
elemDraft.Attributes.Append (attr1);

XmlAttribute attr2 = xml.CreateAttribute ("xmlns:xsd");
attr2.Value = "http://www.w3.org/2001/XMLSchema";
elemDraft.Attributes.Append (attr2);

XmlAttribute attr3 = xml.CreateAttribute ("xsi:type");
attr3.Value = "TravelDraft";
elemDraft.Attributes.Append (attr3);

xml.AppendChild (elemDraft);

//DraftId
XmlElement elemDraftId = xml.CreateElement("DraftId");
elemDraftId.Value = draft.DraftId ?? String.Empty;
elemDraft.AppendChild(elemDraftId);

//StartDate
XmlElement elemStartDate = xml.CreateElement ("StartDate");
elemDraftId.Value = draft.StartDate.Date.ToString() ??
String.Empty;
elemDraft.AppendChild (elemStartDate);
Jan 26 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Have you thought about using xml serialisation (System.Xml.XmlSerialiser)
this means that you wouldn't have to work with an XMlDocument directly...

http://msdn2.microsoft.com/en-us/lib...36(vs.80).aspx

How to: Serialize an Object
http://msdn2.microsoft.com/en-us/lib...4s(VS.80).aspx

How to: Deserialize an Object
http://msdn2.microsoft.com/en-us/lib...9y(VS.80).aspx

How to: Use the XML Schema Definition Tool to Generate Classes and XML
Schema Documents
http://msdn2.microsoft.com/en-us/lib...y7(VS.80).aspx

HTH

Ollie Riches
"Mikus Sleiners" <mi*@newsgroups.nospamwrote in message
news:eO**************@TK2MSFTNGP02.phx.gbl...
>I have a task to create xml document from c# code.

I have example of that document should look like and also a xml schema.
I wonder if i can use this xml schema somehow ?

This is schema:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Draft">
<xs:complexType>
<xs:sequence>
<xs:element name="DraftId" type="xs:string" />
<xs:element name="StartDate" type="xs:dateTime" />
<xs:element name="RetryCount" type="xs:unsignedByte" />
<xs:element name="LocalCurrencyId" type="xs:unsignedByte" />
<xs:element name="EndDate" type="xs:dateTime" />
<xs:element name="OriginalPremium" type="xs:unsignedShort" />
<xs:element name="AuthorizationCode" type="xs:string" />
<xs:element name="Locale" type="xs:string" />
<xs:element name="IssueDate" type="xs:dateTime" />
<xs:element name="StatusCode" type="xs:string" />
<xs:element name="Invoice">
<xs:complexType>
<xs:sequence>
<xs:element name="Number" type="xs:string" />
<xs:element name="IssueDate" type="xs:dateTime" />
<xs:element name="DueDate" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="PolicyNumber" type="xs:unsignedLong" />
<xs:element name="PolicyHolderName" type="xs:string" />
<xs:element name="OriginalCurrencyId" type="xs:unsignedByte" />
<xs:element name="TransactionId" type="xs:string" />
<xs:element name="LocalPremium" type="xs:decimal" />
<xs:element name="PaymentType" type="xs:string" />
<xs:element name="MediatorName" type="xs:string" />
<xs:element name="LobId" type="xs:unsignedByte" />
<xs:element name="PolicyHolder">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Surname" type="xs:string" />
<xs:element name="Code" type="xs:string" />
<xs:element name="BirthDate" type="xs:dateTime" />
<xs:element name="IsPolicyHolder" type="xs:boolean" />
<xs:element name="IsNotResident" type="xs:boolean" />
<xs:element name="Address" type="xs:string" />
<xs:element name="Phone" type="xs:unsignedInt" />
<xs:element name="Email" type="xs:string" />
<xs:element name="Type" type="xs:string" />
<xs:element name="ResidencyCountry" type="xs:unsignedByte" />
<xs:element name="Sex" type="xs:string" />
<xs:element name="Village" type="xs:string" />
<xs:element name="House" type="xs:string" />
<xs:element name="Flat" type="xs:string" />
<xs:element name="TownDistrictOid" type="xs:unsignedByte" />
<xs:element name="RegionCityOid" type="xs:unsignedByte" />
<xs:element name="Street" type="xs:string" />
<xs:element name="PostalIndex" type="xs:unsignedShort" />
<xs:element name="Country" type="xs:unsignedByte" />
<xs:element name="LegalPersonType" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Country" type="xs:string" />
<xs:element name="TravelType" type="xs:unsignedByte" />
<xs:element name="Zone" type="xs:unsignedByte" />
<xs:element name="Program" type="xs:unsignedByte" />
<xs:element name="InsuredPersons">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Surname" type="xs:string" />
<xs:element name="Code" type="xs:string" />
<xs:element name="BirthDate" type="xs:dateTime" />
<xs:element name="IsPolicyHolder" type="xs:boolean" />
<xs:element name="IsNotResident" type="xs:boolean" />
<xs:element name="Type" type="xs:string" />
<xs:element name="ResidencyCountry"
type="xs:unsignedByte" />
<xs:element name="Sex" type="xs:string" />
<xs:element name="TownDistrictOid"
type="xs:unsignedByte" />
<xs:element name="RegionCityOid" type="xs:unsignedByte"
/>
<xs:element name="Country" type="xs:unsignedByte" />
<xs:element name="LegalPersonType"
type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

This is how final document should look like:

<?xml version="1.0" encoding="utf-16"?>
<Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="TravelDraft">
<DraftId>139451de-1bc1-4070-b9d1-548dd4a7f812</DraftId>
<StartDate>2006-10-04T00:00:00</StartDate>
<RetryCount>0</RetryCount>
<LocalCurrencyId>1</LocalCurrencyId>
<EndDate>2006-10-25T23:59:59</EndDate>
<OriginalPremium>38.61</OriginalPremium>
<Locale>lt-LT</Locale>
<IssueDate>2006-10-02T09:26:31.8562127+03:00</IssueDate>
<StatusCode>Unsuccessful</StatusCode>
<Invoice>
<Number>B0002624</Number>
<IssueDate>2006-10-02T09:26:31.8562127+03:00</IssueDate>
<DueDate>2006-10-02T09:26:31.8562127+03:00</DueDate>
</Invoice>
<PolicyNumber>45300000265</PolicyNumber>
<PolicyHolderName>AS AS, 123456-12345</PolicyHolderName>
<OriginalCurrencyId>3</OriginalCurrencyId>
<TransactionId>FVR21tY1Kot1fOfFRqkFJ9WZ7E4=</TransactionId>
<LocalPremium>27.03</LocalPremium>
<PaymentType>Ita</PaymentType>
<MediatorName>Air Baltic Corporation AS</MediatorName>
<LobId>53</LobId>
<PolicyHolder>
<Name>AS</Name>
<Surname>AS</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>false</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Address></Address>
<Phone>11212121</Phone>
<Email>AS**@ASAS.LV</Email>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<Village />
<House>NAMAS</House>
<Flat />
<TownDistrictOid>0</TownDistrictOid>
<RegionCityOid>27</RegionCityOid>
<Street />
<PostalIndex>1212</PostalIndex>
<LegalPersonType>0</LegalPersonType>
</PolicyHolder>
<Country>Estonia</Country>
<Zone>1</Zone>
<TravelType>21</TravelType>
<Program>1</Program>
<InsuredPersons>
<Person>
<Name>AS</Name>
<Surname>AS</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>true</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<LegalPersonType>0</LegalPersonType>
</Person>
<Person>
<Name>AS2</Name>
<Surname>AS2</Surname>
<Code>123456-12345</Code>
<BirthDate>1980-01-01T00:00:00</BirthDate>
<IsPolicyHolder>true</IsPolicyHolder>
<IsNotResident>false</IsNotResident>
<Type>PhysicalPerson</Type>
<ResidencyCountry>0</ResidencyCountry>
<Sex>Male</Sex>
<LegalPersonType>0</LegalPersonType>
</Person>
</InsuredPersons>
</Draft>

This is my code so far (as you can see i'm not using schema, cuz i don't
know how):

XmlDocument xml = new XmlDocument();

XmlDeclaration declaration = xml.CreateXmlDeclaration ("1.0",
utf-16", null);

xml.AppendChild(declaration);

//Root element
XmlElement elemDraft = xml.CreateElement
("Draft");//,"http://www.w3.org/2001/XMLSchema");

//Root element attributes
XmlAttribute attr1 = xml.CreateAttribute ("xmlns:xsi");
attr1.Value = "http://www.w3.org/2001/XMLSchema-instance";
elemDraft.Attributes.Append (attr1);

XmlAttribute attr2 = xml.CreateAttribute ("xmlns:xsd");
attr2.Value = "http://www.w3.org/2001/XMLSchema";
elemDraft.Attributes.Append (attr2);

XmlAttribute attr3 = xml.CreateAttribute ("xsi:type");
attr3.Value = "TravelDraft";
elemDraft.Attributes.Append (attr3);

xml.AppendChild (elemDraft);

//DraftId
XmlElement elemDraftId = xml.CreateElement("DraftId");
elemDraftId.Value = draft.DraftId ?? String.Empty;
elemDraft.AppendChild(elemDraftId);

//StartDate
XmlElement elemStartDate = xml.CreateElement ("StartDate");
elemDraftId.Value = draft.StartDate.Date.ToString() ??
String.Empty;
elemDraft.AppendChild (elemStartDate);


Jan 26 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.