开发者

how to deserialize this xml in Silverlight

Xml is here

<?xml version="1.0" encoding="utf-8"?>
<s>
  <Items>
  <b name="test" width="100">
     <Items>
       <d x="1"/>
       <e width="50"/>
     </Items>
  </b>
  <b name="test2" width="200">
     <Items>
       <d x="2"/>
     </Items>
  </b>
  </Items>
</s>

I'm creating those classes

public class s
{
  public s(){
  Items=new List<b>();
  }
  List<b> Items{get;set;}
}

public class b
{
  public b(){
  Item=new List<object>();
  }
  [XmlAttribute]
  public string name {get;set;}
  [XmlAttribute]
  public int width {get;set;}
}

public class d
{
  public d(){}
  [XmlAttribute]
  public int x {get;set;}
}

public class e
{
  public e(){}
  [XmlAttribu开发者_StackOverflowte]
  public int width {get;set;}
}

And my main code is here

s mainobj=null;
XmlSerializer ser=new XmlSerializer(typeof(s));
mainobj=ser.Deserialize(memoryStream) as s;

Debug.WriteLine(mainobj.Items.Count.ToString());
Debug.WriteLine(mainobj.Items[0].name);
Debug.WriteLine(mainobj.Items[0].Items.Count.ToString());

Output

2
test
0

b object items contains 2 type of object.

How to deserialize this objects.

What's wrong on my code?


My favorite way to understand these sorts of deserialization questions is to test it backwards.

Use the XmlSerializer to SERIALIZE an instance of your expected classes to an XML file, and take a look at the XML that was generated. This should give you some good clues to help you understand what is going on...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜