C# 反序列化XML,其中每个对象都有一个对象列表

C# 反序列化XML,其中每个对象都有一个对象列表,c#,xml,C#,Xml,我正在开发一个WP7应用程序,它可以获取和更新web服务器上的数据。如果任何更新需要响应,我会得到需要处理的错误列表,以及每个错误的可能选择列表。我遇到的问题是为每个对象分配适当的选择列表。到目前为止,我得到了一个错误列表,以及一个针对所有错误的所有可能选择的列表。我希望error对象只包含它的选项列表,这样我就可以处理它了 下面是一个示例响应: <?xml version="1.0" encoding="utf-8"?> <response xmlns:xsd="http:/

我正在开发一个WP7应用程序,它可以获取和更新web服务器上的数据。如果任何更新需要响应,我会得到需要处理的错误列表,以及每个错误的可能选择列表。我遇到的问题是为每个对象分配适当的选择列表。到目前为止,我得到了一个错误列表,以及一个针对所有错误的所有可能选择的列表。我希望error对象只包含它的选项列表,这样我就可以处理它了

下面是一个示例响应:

<?xml version="1.0" encoding="utf-8"?>
<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <response_error_dialogs>
        <error_dialog_list>
            <error_dialog_choice>
                <error_dialog_id>1301</error_dialog_id>
                <error_dialog_message>You have changed the phone number.  Select which phone number to make the primary contact number.</error_dialog_message>
                <error_dialog_title>Phone Number Changed</error_dialog_title>
                <error_dialog_is_set>false</error_dialog_is_set>
                <error_dialog_choice_option_list>
                    <error_dialog_choice_option>
                        <error_dialog_choice_option_id>1</error_dialog_choice_option_id>
                        <error_dialog_choice_option_title>Home</error_dialog_choice_option_title>
                    </error_dialog_choice_option>
                    <error_dialog_choice_option>
                        <error_dialog_choice_option_id>2</error_dialog_choice_option_id>
                        <error_dialog_choice_option_title>Mobile</error_dialog_choice_option_title>
                    </error_dialog_choice_option>
                    <error_dialog_choice_option>
                        <error_dialog_choice_option_id>3</error_dialog_choice_option_id>
                        <error_dialog_choice_option_title>Work</error_dialog_choice_option_title>
                    </error_dialog_choice_option>
                </error_dialog_choice_option_list>
            </error_dialog_choice>
            <error_dialog_choice>
                <error_dialog_id>1303</error_dialog_id>
                <error_dialog_message>You have changed the account email address.  Would you like this to be the new default email?</error_dialog_message>
                <error_dialog_title>Email Address Changed</error_dialog_title>
                <error_dialog_is_set>false</error_dialog_is_set>
                <error_dialog_choice_option_list>
                    <error_dialog_choice_option>
                        <error_dialog_choice_option_id>1</error_dialog_choice_option_id>
                        <error_dialog_choice_option_title>No</error_dialog_choice_option_title>
                    </error_dialog_choice_option>
                    <error_dialog_choice_option>
                        <error_dialog_choice_option_id>2</error_dialog_choice_option_id>
                        <error_dialog_choice_option_title>Yes</error_dialog_choice_option_title>
                    </error_dialog_choice_option>
                </error_dialog_choice_option_list>
            </error_dialog_choice>
        </error_dialog_list>
    </response_error_dialogs>
</response>

因此,在本例中,第一个
error\u dialog\u choice
对象将有一个
列表
,其中包含3个
error\u dialog\u choice\u option
对象,第二个对象有两个
error\u dialog\u choice\u option
对象,以及可能返回的更多对象。感谢您的帮助。谢谢

您可以使用XML序列化更轻松地实现这一点:

var reader = new StringReader(xmlString);
var ser = new XmlSerializer(typeof(Response));
var result = (Response) ser.Deserialize(reader);
使用这些类定义

[XmlType("response")]
public class Response
{
    [XmlElement("response_error_dialogs")]
    public ErrorDialog ErrorDialog;
}

[XmlType("response_error_dialogs")]
public class ErrorDialog
{
    [XmlArray("error_dialog_list")]
    public List<ChoiceErrorDialog> ChoiceList;
}

[XmlType("error_dialog_choice")]
public class ChoiceErrorDialog
{
    [XmlElement("error_dialog_id")]
    public int Id;

    [XmlElement("error_dialog_message")]
    public string Message;

    [XmlElement("error_dialog_title")]
    public string Title;

    [XmlElement("error_dialog_is_set")]
    public bool IsSet;

    [XmlArray("error_dialog_choice_option_list")]
    public List<Option> OptionList;
}

[XmlType("error_dialog_choice_option")]
public class Option
{
    [XmlElement("error_dialog_choice_option_id")]
    public int Id;

    [XmlElement("error_dialog_choice_option_title")]
    public string Title;
}

使用框架中内置的
XmlSerializer
。使用属性映射类及其属性的xml等价物

例如:

[XmlElement("Txn")]
public List<Transaction> Items { get; set; }
[XmlElement(“Txn”)]
公共列表项{get;set;}
您可以使用XSD.exe生成XSD,然后为XSD生成C#代码。将Visual Studio命令提示符与示例response.xml文件一起使用。例如:

c:>xsd response.xml


c:>xsd response.xsd/c/edb

使用XmlSerializer是否更容易?你甚至可以使用xsd.exe从示例XML生成类型…同意Marc使用XmlSerializer。不,你比我快了1分钟!但答案非常全面。:)我没有说所有的细节。马库斯,你太棒了。非常感谢。
[XmlType("response")]
public class Response
{
    [XmlElement("response_error_dialogs")]
    public ErrorDialog ErrorDialog;
}

[XmlType("response_error_dialogs")]
public class ErrorDialog
{
    [XmlArray("error_dialog_list")]
    public List<ChoiceErrorDialog> ChoiceList;
}

[XmlType("error_dialog_choice")]
public class ChoiceErrorDialog
{
    [XmlElement("error_dialog_id")]
    public int Id;

    [XmlElement("error_dialog_message")]
    public string Message;

    [XmlElement("error_dialog_title")]
    public string Title;

    [XmlElement("error_dialog_is_set")]
    public bool IsSet;

    [XmlArray("error_dialog_choice_option_list")]
    public List<Option> OptionList;
}

[XmlType("error_dialog_choice_option")]
public class Option
{
    [XmlElement("error_dialog_choice_option_id")]
    public int Id;

    [XmlElement("error_dialog_choice_option_title")]
    public string Title;
}
xsd.exe /?
svcutil.exe /?
[XmlElement("Txn")]
public List<Transaction> Items { get; set; }