开发者

deserialize the XML Document --- Need Help

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.


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 { }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜