all groups > dotnet xml > november 2003 >
You're in the

dotnet xml

group:

xsd.exe and the resulting cs classes.


xsd.exe and the resulting cs classes. Iain
11/26/2003 7:46:06 PM
dotnet xml: I'm embarking on a c# based project in which I would like to make use of a
lot of XML including database access and using hte xml and .net tools as a
coding shortcut.

What I'm trying to understand at the moment is how to use xsd.exe and result
..cs files to mange the xml.

First question.

My XSD includes <sequence>s which have > 1 value. the cs produced renders
this as a single instance, though if I change this to an array (manually)
the deserialization works as expected (nice!). How can I tell xsd it SHOULD
be an array (I've tried setting maxOccurs, but this seems to make no
difference).

Second question.

I want some of the attributes (and elements for that matter) to be
mandatory. Specifically, I would like an exception to be throw during
deserialisation (oops sorry about the 's' chaps - I speak British English as
a first tongue - the 'z' thing takes some getting used to!) if an attribute
is not present. How do I do this?

Equally, I would like an exception thrown if an attribute or element NOT
specified in the XSD occurs.

In both cases, I'm kind of hoping I can do it without having to make
post-xsd.exe changes to the source.

Look forward to feedback!

iain

Re: xsd.exe and the resulting cs classes. Dino Chiesa [Microsoft]
11/26/2003 8:59:58 PM
xsd makes an array when you have maxOccurs="unbounded", eg passing the
following xsd into xsd.exe gives me an array:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" id="Ionic">
<xs:complexType name="Root">
<xs:sequence>
<xs:element name="Stamp" maxOccurs="unbounded" type="xs:date"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Something" type="Root"/>
</xs:schema>


For Q2, when deserializing, pass the Deserialize() method an instance of
XmlValidatingReader.

Here is an example:

XmlReader xr = new XmlTextReader(fileName);
XmlValidatingReader vr = new XmlValidatingReader(xr);
vr.Schemas.Add("urn:example-org:person", "person.xsd");
Person p = (Person)ser.Deserialize(vr);

ref:
http://msdn.microsoft.com/msdnmag/issues/02/11/XMLFiles/default.aspx

-Dino

--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ o n l i n e . m i c r o s o f t . c o m


[quoted text, click to view]

Re: xsd.exe and the resulting cs classes. Iain
11/27/2003 9:51:12 AM
Thank you very much.

I had a maxOccurs, but in the wrong place (I'm still getting to grips with
xsd - it's one of those things that sounds obvious, but turns out to be VERY
complicated in detail!).

And the other trick is exactly what I'm looking for!

Iain
[quoted text, click to view]

AddThis Social Bookmark Button