1

I am hoping someone can help me with this.

I need to serialize a request from XML so that it can be converted into a SOAP call. The problem is, I am having trouble succeeding when the xml is missing a xmlns definition.

First of all, I have a class called GetRegistrationStatusRequest. This was auto generated using the XSD tool from a customer provided XSD. The class looks as follows (Note that I have replaced the client sensitive URL)

// 
// This source code was auto-generated by xsd, Version=4.0.30319.18020.
//

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://some.url/srvc/getregistrationstatus/v1_0/request/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://some.url/srvc/getregistrationstatus/v1_0/request/", IsNullable=false)]
public partial class GetRegistrationStatusRequest : ServiceRequest {
    ... auto generated code

As mentioned, this is auto generated. I have included it for reference.

My Code for actually deserializing the Xml is as follows:

XmlSerializer serializer = new XmlSerializer(typeof(GetRegistrationStatusRequest));
GetRegistrationStatusRequest request = (GetRegistrationStatusRequest)serializer.Deserialize(new StringReader(RequestXml.OuterXml));

If I use XML as follows, it works fine:

<GetRegistrationStatusRequest xmlns="http://some.url//srvc/getregistrationstatus/v1_0/request/">
  <PlateNo>PlateNo1</PlateNo>
</GetRegistrationStatusRequest>

However the XML that I am receiving (which is out of my control) does not have the xmlns defined. Instead it looks like this:

<GetRegistrationStatusRequest>
  <PlateNo>PlateNo1</PlateNo>
</GetRegistrationStatusRequest>

When I attempt to deserialize this I get the message "The is an arror in XML document (1,2)"

I am wondering if it is actually possible to get the Xml to deserialize without needing the xmlns definition?

Any help would be greatly appreciated.

user1166145
  • 193
  • 3
  • 10
  • I think another post will answer your question :http://stackoverflow.com/questions/2296305/deserialize-xml-without-namespaces-but-in-a-class-expecting-namespaces – Seminda Sep 02 '14 at 02:48

2 Answers2

0

OK, I found the solution here, I hope it helps anyone else who has this issue:

http://calvinirwin.net/2011/02/10/xmlserialization-deserialize-causes-xmlns-was-not-expected/

My code now looks like this:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "GetRegistrationStatusRequest";
Root.IsNullable = true;

XmlSerializer serializer = new XmlSerializer(typeof(GetRegistrationStatusRequest), xRoot);
request = (GetRegistrationStatusRequest)serializer.Deserialize(new StringReader(RequestXml.OuterXml));
user1166145
  • 193
  • 3
  • 10
0

I had the same problem. Initially I tried using an XmlTextReader with the Namespaces property set to false. That code worked fine in a client application, but still encounters the error when used within the WCF service where the relevant type is defined.

The solution I found was to use an XmlDocument object and then correct the xmlns attribute before deserializing:

string ns = "http://some.url/srvc/getregistrationstatus/v1_0/request/";
XmlDocument doc = new XmlDocument();
doc.LoadXml(RequestXml.OuterXml);
doc.DocumentElement.SetAttribute("xmlns", ns);
XmlSerializer ser = new XmlSerializer(typeof(GetRegistrationStatusRequest), ns);
using (StringReader sr = new StringReader(doc.InnerXml)) {
    GetRegistrationStatusRequest req = (GetRegistrationStatusRequest)ser.Deserialize(sr);
}

Note also the use of using to close and dispose of the StringReader.

Bloopy
  • 305
  • 1
  • 9