Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML deserializing only works with namespace in xml

The most simple way I get ServiceStack xml deserialization to work is when the xml contains a namespace. However, the xml I receive do not contain namespaces. The most simple working example:

[Serializable]
public class test
{

}

class Program
{
   static void Main(string[] args)
   {
       string xml="<test xmlns=\"http://schemas.datacontract.org/2004/07/\"></test>";
       var result = ServiceStack.Text.XmlSerializer.DeserializeFromString<test>(xml);
   }
}

However, that is not what I want. I want the following to deserialize, since that is the xml I get from several services:

string xml="<test></test>";

But that gives me the following error:

DeserializeDataContract: Error converting type: Error in line 1 position 7. 
Expecting element 'test' from namespace 
'http://schemas.datacontract.org/2004/07/'.. 
Encountered 'Element'  with name 'test', namespace ''.

I tried:

[Serializable]
[XmlRoot("test", Namespace = "")]
public class test

I can't create a new Serializer, because ServiceStack.Text.XmlSerializer is static. I need to choose for either Microsoft XmlSerializer OR ServiceStack (not both). Meaning: if I can't get this simple example to work I need to skip an otherwise very useful part of the ServiceStack package. The last thing I want is to inject some dummy namespace in the incoming xml.

like image 483
user1154148 Avatar asked Mar 13 '12 18:03

user1154148


People also ask

What is Deserializing XML?

Deserialization is the process of reading an instance of an XML document and constructing an object that is strongly typed to the XML Schema (XSD) of the document. Before deserializing, an XmlSerializer must be constructed using the type of the object that is being deserialized.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization. Note this is the kind of thing I meant. You are not telling the XmlSerializer to ignore namespaces - you are giving it XML that has no namespaces.

Is a namespace for XML serialization?

Serialization namespace contains several Attribute classes that can be applied to members of a class. For example, if a class contains a member that will be serialized as an XML element, you can apply the XmlElementAttribute attribute to the member.

Which class is used for serializing and deserializing XML data?

XmlSerializer Class (System. Xml. Serialization)


1 Answers

ServiceStack uses .NET's Xml DataContractSerializer to serialize XML to remove Namespaces you need to either set the Namespace to an empty string with:

[DataContract(Namespace="")]
public class test { ... }

But then you'll have to mark each property you want serialized with [DataMember] attributes. A better option is to specify an empty namespace for all types under a C# namespace by adding and Assembly attribute in your Assembly.cs file, e.g:

[assembly: ContractNamespace("", ClrNamespace = "MyServiceModel.DtoTypes")]

Note: you can remove the [Serializable] attribute - it's not used by any of ServiceStack's serializers. Also all XmlSerializer attributes like [XmlRoot] are useless since ServiceStack uses .NET's DataContractSerializer not Microsoft's earlier XmlSerializer.

like image 96
mythz Avatar answered Sep 29 '22 12:09

mythz