Jump to content

Dot Net Help - Wcf + Xml Experts


Recommended Posts

Posted

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.datacontract.org/2004/07/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.datacontract.org/2004/07/MyProjectName' is not expected. Expecting element 'middleName'.; 
 
 
 
Can you guys please help me if i am missing anything... or need more info on this.
 
 
Posted

 

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.datacontract.org/2004/07/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.datacontract.org/2004/07/MyProjectName' is not expected. Expecting element 'middleName'.; 
 
 
 
Can you guys please help me if i am missing anything... or need more info on this.

 

http://stackoverflow.com/questions/2466804/datacontractserializer-and-deserializing-web-service-response-types

 

check this..   first you have to add namespace to     new DataContractSerializer(typeof(Person));      

 

https://msdn.microsoft.com/en-us/library/ms404902(v=vs.110).aspx

 

and aa response lo edanna xml end tags ki namespace undo chudu.. seems like   xml end tags   </response>   shouldn't have namesapce 

Posted

Found solution to my question:

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...

×
×
  • Create New...