English 中文(简体)
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?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签