467,206 Members | 1,406 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,206 developers. It's quick & easy.

XML serializatin vs. an XSD schema

Hi there,

I have a C# object heirarchy and an XSD document to which I wish to
match the serialized objects in that heirarchy. I have an abstract
parent class called "Test" which has no properties, and a set of
derived classed which implement specific functionality, for example
"TestUserOnline", "TestUserOnPC" etc. Each of these has properties
which correspond to the arguments of that operation. For example,
"TestUserOnline" has a "user" property, "TestUserOnPC" has a "user"
property and a "pc" property. I want these to serialise so that in the
xml these appear as a "Test" element, with a single element beneath it
for the specifics of this operation, like this

....
<Test>
<TestUserOnPc user="bob" pc="bobsPC"/>
</Test>
....

This is the way its done in the XSD file, and I'd like to get the XSD
file matching the output so I can use the XSD file to give me
intellisense when I'm writing XML to represent these objects.

I've used XMLInclude's for the types which I want to count as "Test"s,
and what I'm getting when I serilaize is this.

....
<Test xsi:type="TestUserOnPC" user="bob" pc="bobsPC"/>
....

Obv this isn't what I want, as I won't get any intellisense to give me
only "user" and "pc" on this type of test. So the question is, is
there anyway to use XML attributes etc. to serialize the objects into
the kind of form I want, or another form which would allow
intellisense?

Ta,
Steve.
Nov 12 '05 #1
  • viewed: 2311
Share:
1 Reply
One way to do this is to infer the schema from a sample XML file.
you can do this with the xsd.exe utility.
It will generate and XSD from the sample XML you provide.
You can then iterate on the XSD, modifying it as you see fit.
Then serialize, and see if it works for you.

For example, running xsd.exe on the following xml file:
<Collection>
<Test>
<TestUserOnPc user="bob" pc="bobsPC"/>
</Test>
<Test>
<TestUserOnline user="mary"/>
</Test>
<Test>
<TestUserOnline user="bob"/>
</Test>
</Collection>
Gives me this xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Collection" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Collection" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element name="TestUserOnPc" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="user" type="xs:string" />
<xs:attribute name="pc" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="TestUserOnline" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="user" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
You can change maxOccurs, or change the targetnamespace, or ....
Given this XSD, you should be able to produce something that VS.NET can use
for Intellisense, as per
http://msdn.microsoft.com/library/en...ckgrounder.asp

http://staff.develop.com/candera/web...b-24113a9a8a58
-Dino
"Steve Morrell" <St***********@techprt.co.uk> wrote in message
news:3d**************************@posting.google.c om...
Hi there,

I have a C# object heirarchy and an XSD document to which I wish to
match the serialized objects in that heirarchy. I have an abstract
parent class called "Test" which has no properties, and a set of
derived classed which implement specific functionality, for example
"TestUserOnline", "TestUserOnPC" etc. Each of these has properties
which correspond to the arguments of that operation. For example,
"TestUserOnline" has a "user" property, "TestUserOnPC" has a "user"
property and a "pc" property. I want these to serialise so that in the
xml these appear as a "Test" element, with a single element beneath it
for the specifics of this operation, like this

...
<Test>
<TestUserOnPc user="bob" pc="bobsPC"/>
</Test>
...

This is the way its done in the XSD file, and I'd like to get the XSD
file matching the output so I can use the XSD file to give me
intellisense when I'm writing XML to represent these objects.

I've used XMLInclude's for the types which I want to count as "Test"s,
and what I'm getting when I serilaize is this.

...
<Test xsi:type="TestUserOnPC" user="bob" pc="bobsPC"/>
...

Obv this isn't what I want, as I won't get any intellisense to give me
only "user" and "pc" on this type of test. So the question is, is
there anyway to use XML attributes etc. to serialize the objects into
the kind of form I want, or another form which would allow
intellisense?

Ta,
Steve.

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by wooks | last post: by
1 post views Thread by Hari Om | last post: by
4 posts views Thread by Gordon Dickens | last post: by
4 posts views Thread by Iain A. Mcleod | last post: by
reply views Thread by Derek | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.