简体   繁体   中英

XML-RPC struct to C# class with dynamic named structs

I am trying to map an XML-RPC struct into a C# class. I am using XmlRpcLight from github. Below is the XML-RPC being passed:

<?xml version=""1.0"" encoding=""ISO-8859-1""?>
<methodCall>
    <methodName>namespace.method</methodName>
    <params>
        <param>
            <value>
                <struct>
                    <member>
                        <name>OrderID</name>
                        <value>
                            <int>1</int>
                        </value>
                    </member>
                    <member>
                        <name>OrderItems</name>
                        <value>
                            <struct>
                                <member>
                                    <name>0</name>
                                    <value>
                                        <struct>
                                            <member>
                                                <name>ItemID</name>
                                                <value>
                                                    <string>12345</string>
                                                </value>
                                            </member>
                                        </struct>
                                    </value>
                                </member>
                                <member>
                                    <name>1</name>
                                    <value>
                                        <struct>
                                            <member>
                                                <name>ItemID</name>
                                                <value>
                                                    <string>23456</string>
                                                </value>
                                            </member>
                                        </struct>
                                    </value>
                                </member>
                            </struct>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
   </params>
</methodCall>

The classes i am using to try and map this is as follows:

public class OrderItem
{
    public string ItemID;
}

public class OrderItems 
{
   public OrderItem[] OrderItem;
}

public class OrderData
{
    public int OrderID;
    public OrderItems OrderItems;
}

The error message i am receiving back is as follows.

request contains struct value with missing non-optional member: OrderItem [request : parameter 2 : struct mapped to type OrderData : member OrderItems mapped to type OrderItems : struct mapped to type OrderItems]

I can't work out how to map this because the Order Item names are a dynamic number. I have tried arrays, arrays of arrays but nothing seems to let it go through. The only way i have got it to work is to use an XmlRpcStruct type in place of a custom class for "OrderItems". While this works it isn't the result i want.

Can anybody please help me understand how these XMl-RPC types can be mapped to C#?

Thanks so much in advance!

Have you tried using the Paste-Special to generate your classes for you?

https://stackoverflow.com/a/14700981/112397


The c# generated off your xml is horrible, but I can kinda see what its trying to do, have you tried something like this?

EDIT: I think you might be missing a level of inheritance.

public class Item
{
    public string ItemID;
}

public class OrderItem
{
    public Item[] Items;
}

public class OrderItems 
{
   public OrderItem[] OrderItem;
}

public class OrderData
{
    public int OrderID;
    public OrderItems OrderItems;
}

Your array of arrays comment struck me as I read this answer , thought I would generate that code myself and see if I saw anything like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM