views:

451

answers:

2

I am using the below code snippet now to deserialize the XML document ...

[WebMethod]
public XmlDocument OrderDocument(XmlDocument xmlDoc)
{
   XmlSerializer serializer = new XmlSerializer(typeof(sendOrder.Order));

   string xmlString = xmlDoc.OuterXml.ToString();

   byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);

   MemoryStream ms = new MemoryStream(buffer);

   sendOrder.Order orderDoc = (sendOrder.Order)serializer.Deserialize(ms);

   sendOrder.WebService_ConsureWebService ws = 
         new sendOrder.WebService_ConsureWebService();

   ws.Operation_1(ref orderDoc);

   return xmlDoc;
}

Can anybody please tell what is wrong with the code, as the error says there is an error in the XML document but if you check the document I am passing and the even the Order object its got the same structure and the namespace

There is an error in XML document (1, 2). ---> System.InvalidOperationException: http://ConsureWebService.Order'> was not expected.

+1  A: 

I would guess that it is a namespace issue (i.e. xml namespaces). Can you show example xml and the Order class?

For info, you can read from an XmlDocument "as is", via:

sendOrder.Order orderDoc;
using(XmlReader reader = new XmlNodeReader(xmlDoc.DocumentElement)) {
    orderDoc = (sendOrder.Order) serializer.Deserialize(reader);
}

Much simpler than messing with encoding and streams...


With your sample xml/code, you can fix this by adding:

[XmlRoot(Namespace = "ConsureWebService.Order")]

to the class. If the class advertises itself as a partial class you can even do this in a separate code file, so you don't need to edit the generated code. This would be (in the correct namespace):

[XmlRoot(Namespace = "ConsureWebService.Order")]   
public partial class Order { }
Marc Gravell
The XML document I am passing is as below<ns0:Order xmlns:ns0="http://ConsureWebService.Order"><OrderId>OrderId_0</OrderId><OrderName>OrderName_0</OrderName></ns0:Order>
Howard
Also, this is the sendOrder.Order object I am trying to deserialize to:namespace sendOrder{ [Serializable] [XmlType(AnonymousType = true, Namespace = "http://ConsureWebService.Order")] [GeneratedCode("System.Xml", "2.0.50727.3053")] [DebuggerStepThrough] [DesignerCategory("code")] public class Order { public Order(); public string OrderId { get; set; } public string OrderName { get; set; } }}
Howard
XmlSerializer deserializer = new XmlSerializer(typeof(sendOrder.Order)); sendOrder.Order orderDoc; using (XmlReader reader = new XmlNodeReader(xmlDoc.DocumentElement)) { orderDoc = (sendOrder.Order)deserializer.Deserialize(reader); }using the above code snippet, while debugging I am getting null value for the reader thou, I can find xml document for the xmlDoc
Howard
The order file meta data file, I am trying to consume an exposed BTS webs ervice ..................................................................................................................................namespace sendOrder{ [Serializable] [XmlType(AnonymousType = true, Namespace = "http://ConsureWebService.Order")] [GeneratedCode("System.Xml", "2.0.50727.3053")] [DebuggerStepThrough] [DesignerCategory("code")] public class Order { public Order(); public string OrderId { get; set; } public string OrderName { get; set; } }
Howard
A: 
<ns0:Order xmlns:ns0="ConsureWebService.Order">; 
      <OrderId>OrderId_0</OrderId> 
      <OrderName>OrderName_0</OrderName> 
 </ns0:Order>

This doesn't appear to be a fully valid xml document, can you post up the whole thing? It is kind of like giving someone only the first line of a stack trace and saying "Well!" .

EDIT

Here is a guess: http://support.microsoft.com/kb/816225

Do you have a default constructor?

Woot4Moo
NO this the complete XML document that I am passing and trying to deserialze to the sendOrder.Order object ... both I have posted as a comment earlier
Howard
And If you notice even the namespace and structure of the document is same ... I am clueless as to why the below error is coming ..System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <Order xmlns='http://ConsureWebService.Order'> was not expected.
Howard
I am trying deserialize the sendOrder.Order Object which does have default constructurnamespace sendOrder { [Serializable] [XmlType(AnonymousType = true, Namespace = "ConsureWebService.Order")] [GeneratedCode("System.Xml", "2.0.50727.3053")] [DebuggerStepThrough] [DesignerCategory("code")] public class Order { public Order(); public string OrderId { get; set; } public string OrderName { get; set; } } }
Howard