Menu

NullableTypes and XmlSerialization

Help
Ricardo
2008-07-07
2013-04-09
  • Ricardo

    Ricardo - 2008-07-07

    Hi. I have a class with attributes that use the NullableTypes classes:

    [Serializable]
    public class Testing
    {
        NullableInt32 x;
        NullableString y;

        public NullableInt32 X
        {
            get { return x; }
            set { x = value; }
        }

        public NullableString Y
        {
            get { return y; }
            set { y = value; }
        }
    }

    This class is on a web service and I need to return a big array of this class. So I'm serializing the array in a xml file and I'm sending it to a Windows Forms application using the WSE 2.0. Here is the web service method:

    [WebMethod(EnableSession=true)]
    public void GetTest()
    {
        Testing[] tests = new Testing[3];
        tests[0] = new Testing();
        tests[1] = new Testing();
        tests[2] = new Testing();
        tests[0].X = 000;
        tests[0].Y = "zero";
        tests[1].X = 111;
        tests[1].Y = "one";
        tests[2].X = 222;
        tests[2].Y = "two";
       
        string basePath = AppDomain.CurrentDomain.BaseDirectory;
        string fileName = basePath + "test.txt";

        FileStream fileRet = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Testing[]), new Type[] { typeof (NullableInt32), typeof(NullableString) });
        serializer.Serialize(fileRet, tests);

        fileRet.Close();

        FileStream compressedFile = new FileStream(fileName, FileMode.Open);
        byte[] buffer = new byte[compressedFile.Length];
        compressedFile.Read(buffer, 0, Convert.ToInt32(compressedFile.Length));
        compressedFile.Close();

        ResponseSoapContext.Current.Attachments.Add(new DimeAttachment("attachment",  "application/octectstream", TypeFormat.Unknown, new MemoryStream(buffer)));
    }

    The generated file looks like this:
    <?xml version="1.0"?>
    <ArrayOfTesting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Testing>
        <X>0</X>
        <Y>zero</Y>
      </Testing>
      <Testing>
        <X>111</X>
        <Y>one</Y>
      </Testing>
      <Testing>
        <X>222</X>
        <Y>two</Y>
      </Testing>
    </ArrayOfTesting>

    This file is sent to the client application correctly and arrives exactly as above. But here comes the problem: when I try to deserialize this file into the array, all the properties have the null value (testing[0].X.IsNull returns true, as well the Y property).
    Here is the client code:

    WebService.ServiceWse service = new ServiceWse();
    service.GetTest();
    if (service.ResponseSoapContext.Attachments.Count > 0)
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Testing[]), new Type[] { typeof (NullableInt32), typeof(NullableString) });
        tests2 = (Testing[])serializer.Deserialize(service.ResponseSoapContext.Attachments[0].Stream);

        //Here is where tests[0].X.IsNull returns true
    }

    The WSE works perfectly, but I don't know how to load the correct values in the client application. Does anyone know what I'm doing wrong?

    Thanks
    Ricardo

     
    • Nobody/Anonymous

      Hi, wich version of nullabletypes are you using ?

       
    • Ricardo

      Ricardo - 2008-07-08

      My version is 1.2.1604.27693.

       
      • Nobody/Anonymous

        Please do run the tests with SaveTestsReport.BAT in tests folder and posts the result.

        Also try with the latest 1.3.1 version and let me know

        bye (luKa)

         

Log in to post a comment.

MongoDB Logo MongoDB