.NET Framework Bookmark and Share   
 index > ASMX Web Services and XML Serialization > Deserializing when there is no element representing the container
 

Deserializing when there is no element representing the container

My XML looks like this:

<root>
   <box id="1">
      <code>xxx</code>
      <thing name="a"/>
      <thing name="b"/>
   </box>
   <box id="2">
      <code>yyy</code>
      <thing name="c"/>
      <thing name="d"/>
   </box>
</root>


How do I attribute these classes (see classes below) so that I can deserialize from the above xml?

class Root
{
   List<Box> boxes;
}
class Box
{
   int id;
   string code;
   List<Thing> things;
}
class Thing
{
   string name;
}


The issue is that there is no xml container element in the incoming xml. there's no <boxes> element in a root, and no <things> element in a box. (the schema is already defined this way, it's not an option to change it. I just want to know how to deserialize from it when there is no container element)

How should I assign attributes to these classes for deserialization?

I have tried messing around with the XmlArray and XmlArrayItem attributes, but don't get it yet from the examples on the MSDN Library docs.
Wyck
Try putting [XmlElement] on thing.


John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
  • Marked As Answer byWyck Thursday, September 10, 2009 7:24 PM
  •  
John Saunders
Try putting [XmlElement] on thing.


John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
  • Marked As Answer byWyck Thursday, September 10, 2009 7:24 PM
  •  
John Saunders
OMG! That was incredibly easy, but very counterintuitive.

Surprising bit of magic -- to attribute the container with XmlElement then somehow the elements become items in the container. That seems weird to me. I was expecting if I put the XmlElement attribute on the container then it would make the container be serialized as an element, which is the exact opposite of what I wanted. I guess that's why it didn't occur to me to try.

But thanks, John! I learned something useful today!
Wyck

You can use google to search for other answers

Custom Search

More Threads

• asmx page cannot be found!
• Capturing local traffic with Fiddler to a webservice
• Returning an ArrayList from a WS to AJAX page
• Error while running web services
• Web Services Licensing
• Create HttpRequest / HttpWorkerRequest from HttpContext.Request.SaveAs(...) file
• Xml reference is Nothing in Framework 2
• Can you pass a user-defined type into a web method?
• how to fill a datagrid by filling a datatable in a webmethod and calling by its behavior
• Cannot view XML input using XSL style sheet