none
Serializing Service request and again Deserializing to service request object C# RRS feed

  • Question

  • Hi Guys,

    I need help in solving a scenario.

    I have a service, the request object is a bit complex. I am using DataContractSerializer to convert the whole request type(MessageContract) to convert to string.

    While it creates the object to xml format (string) it creates a namespace by that project name (i.e. http://schemas.datac...7/MyProjectName).

    we right this to db. I am ok with that.

     

    In the another process, i need to re-use the above data from db and need to deserialise from string to the same class type. Here i generated the same class using xsd, i gave the same namespace (at class level) so it matches the one it created above process. I am Deserailaising using below code.

     

    My Issue is one of the field (middleName) in Person class is empty and it has nullable value to true in annotation, but it throws below error.

     

    option 1: 

               var serializer = new DataContractSerializer(typeof(Person));
     
                Person response;
     
                using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
                {
                    response = (Person)serializer.ReadObject(ms);
                }
                return response;
     
    The error i am getting is 
    An error occurred while saving your changes: Error in line 1 position 183. 'EndElement' 'Person' from namespace 'http://schemas.datac...7/MyProjectName' is not expected. Expecting element 'middleName'.; 
     
     
     
    Can you guys please help me if i am missing anything... or need more info on this.

    Wednesday, December 9, 2015 10:10 PM

Answers

  • Found solution to my query:

    When i get the service request, if i serialize using the below code:

    SERIALIZATION:

    private static readonly XmlSerializerNamespaces Namespaces;
    
    public static string SerializeWithoutNamespaces(Type typ, object svcRequestObj)
            {
                var serializer = new XmlSerializer(typ, "");
                var output = new StringBuilder();
                var settings = new XmlWriterSettings
                {
                    OmitXmlDeclaration = true
                };
                using (var writer = XmlWriter.Create(output, settings))
                {
                    serializer.Serialize(writer, svcRequestObj, Namespaces);
                }
    
                var result = output.ToString();
                return result;
            }

    And to DE-SERAIALIZE: (COMMON ONE)

    private static T SerializeFromString<T>(string xml)
            {
                var serializer = new XmlSerializer(typeof(T));
    
                using (var reader = new StringReader(xml))
                {
                    return (T)serializer.Deserialize(reader);
                }
            }        
    This helped me smoothly...


    • Marked as answer by Venky_Ferrari Thursday, December 10, 2015 8:06 PM
    • Edited by Venky_Ferrari Thursday, December 10, 2015 8:08 PM
    Thursday, December 10, 2015 8:06 PM

All replies

  • Found solution to my query:

    When i get the service request, if i serialize using the below code:

    SERIALIZATION:

    private static readonly XmlSerializerNamespaces Namespaces;
    
    public static string SerializeWithoutNamespaces(Type typ, object svcRequestObj)
            {
                var serializer = new XmlSerializer(typ, "");
                var output = new StringBuilder();
                var settings = new XmlWriterSettings
                {
                    OmitXmlDeclaration = true
                };
                using (var writer = XmlWriter.Create(output, settings))
                {
                    serializer.Serialize(writer, svcRequestObj, Namespaces);
                }
    
                var result = output.ToString();
                return result;
            }

    And to DE-SERAIALIZE: (COMMON ONE)

    private static T SerializeFromString<T>(string xml)
            {
                var serializer = new XmlSerializer(typeof(T));
    
                using (var reader = new StringReader(xml))
                {
                    return (T)serializer.Deserialize(reader);
                }
            }        
    This helped me smoothly...


    • Marked as answer by Venky_Ferrari Thursday, December 10, 2015 8:06 PM
    • Edited by Venky_Ferrari Thursday, December 10, 2015 8:08 PM
    Thursday, December 10, 2015 8:06 PM
  • Hi Venky_Ferrari,

    Thanks for sharing your solution.

    Best Regards,

    Wanjun Dong


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, December 11, 2015 1:52 AM
    Moderator