开发者

RestSharp Deserialize a List in a List

开发者 https://www.devze.com 2023-03-27 11:33 出处:网络
i have some trouble getting this to work: my xml looks like: <root> <item> <id></id>

i have some trouble getting this to work:

my xml looks like:

<root>
    <item>
        <id></id>
        <name></name>
        <CollectionProp>
            <item>
                <id></id>
                <name></name>
            </item>
        </CollectionProp>
    </item>
</root>

my class looks like:

public class Item
{
    public int id { get; set; }
    public string name { g开发者_如何转开发et; set; }
    public List<CollectionProp> CollectionProp { get; set; }
}

and another one:

public class CollectionProp
{
    public int id { get; set; }
    public string name { get; set; }
}

i also tried to wrap another class around CollectionProp

CollectionPropCollection : List<CollectionProp>

Also tried: CollectionPropCollection class with / or with a property "item" of type CollectionProp.

here's my execute statement:

var result = client.Execute<List<Item>>(request);

EDIT Aug 19:

ok, my post may not clearly pointed out my problem. so now i think i broke down the problem to the following:

the XmlDeserializer has a problem to distinguish between the item under and the item under

so the response has 2 items under and each has 3 items under , my deserialized result has 8 objects. the first with values, the next 3 with properties set to null, the 4th with values and so on....

any idea how i can solve this in a way like : item under needs to be parsed into objectA, item under , which is a child of item under root, needs to be parsed into objectB ?


If the xml file is a serialized Item you can do this:

XmlSerializer serializer = new XmlSerializer(typeof(Item)); //or if you have a item variable item.GetType()
FileStream stream = new FileStream(pathToFile, FileMode.Open, FileAccess.ReadWrite);
Item item = (Item) serializer.Deserialize(stream);
stream.Close();

UPDATE

When it's a list of Items you just replace typeof(Item) with typeof(List<Item>) and you cast it to a List<Item>

0

精彩评论

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

关注公众号