Ideas & comments about XSD2Code.

Jul 20, 2007 at 1:21 PM
Hello,
Do not hesitate to forward me your ideas of evolution.
Thank you.
Psacal.
Oct 14, 2008 at 8:49 PM
I have just generated a set of classes for an xml schema and the classes look quite usable. Only one thing is not quite like I hoped. I have a complex type as follows:

<xs:complexType name="multipleChoice">
    <xs:sequence>
        <xs:element name="selectedOptions">
            <xs:simpleType>
                <xs:list itemType="xs:integer" />
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

This includes a list. I would be nice to be able to manipulate the list by having methods Append(int value), etc. Now, I must specify something like: .selectedOptions = "1 2 3";

One thing is not clear to me. How can I serialize and deserialize the class instance to and from an XML string or stream?

 

Oct 15, 2008 at 10:05 AM
Edited Oct 15, 2008 at 10:15 AM
Hello, thanks for you post.
Try this :


    5
  <xs:complexType name="multipleChoice">

    6     <xs:sequence maxOccurs="unbounded">

    7       <xs:element name="selectedOptions">

    8         <xs:simpleType>

    9           <xs:list itemType="xs:integer" />

   10         </xs:simpleType>

   11       </xs:element>

   12     </xs:sequence>

   13   </xs:complexType>

 

With this change, Xsd2Code generate a generic collection (use List<T> in the add-in)

 For XML include this code in partial class :

   73         public static bool FromXML(string xml, out ClassName obj, out string ErrorMessage)

   74         {

   75             obj = null;

   76             ErrorMessage = "";

   77             try

   78             {

   79                 StringReader stringReader = new StringReader(xml);

   80                 XmlTextReader xmlTextReader = new XmlTextReader(stringReader);

   81                 XmlSerializer xmlSerializer = new XmlSerializer(typeof(ClassName));

   82                 if (xmlSerializer.CanDeserialize(xmlTextReader))

   83                 {

   84                     obj = (ClassName)xmlSerializer.Deserialize(xmlTextReader);

   85                     return true;

   86                 }

   87                 else

   88                 {

   89                     ErrorMessage = "CanDeserialize failed";

   90                     return false;

   91                 }

   92             }

   93             catch (Exception e)

   94             {

   95                 ErrorMessage = e.Message;

   96                 return false;

   97             }

   98         }



 

   56         public string ToXml()

   57         {

   58             string rawXml = "";

   59             XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());

   60             using (MemoryStream memoryStream = new MemoryStream())

   61             {

   62                 //Sérialisation

   63                 xmlSerializer.Serialize(memoryStream, this);

   64 

   65                 //Lecture du flux

   66                 memoryStream.Seek(0, SeekOrigin.Begin);

   67                 using (StreamReader streamReader = new StreamReader(memoryStream))

   68                     rawXml = streamReader.ReadToEnd();

   69             }

   70             return rawXml;

   71         }

Pascal.

 

 

 

 

Oct 16, 2008 at 11:32 AM
Hi Pascal,

Thanks for your swift reply.

Your solution will of course work, but will make my XML documents a lot larger, as many options may be in each list and this would be a draw-back in my case. Please, consider the feed-back as a feature request for support for simpleType list.

Your (De)Serialize examples are very helpful. I was missing the point that the framework can do this because of all the attributes added to the code. I was looking for methods in the generated class for this.

Regards,

Roel