2

私は、Web サーバー上のデータを取得および更新する WP7 アプリに取り組んでいます。更新に応答が必要な場合は、対処する必要があるエラーのリストと、各エラーの可能な選択肢のリストを取得します。私が抱えている問題は、各オブジェクトに適切な選択肢のリストを割り当てることです。今のところ、エラーのリストと、すべてのエラーに対して可能なすべての選択肢の別のリストを取得しています。エラーオブジェクトにオプションのみのリストを含めて、それを処理できるようにしたいと思います。

したがって、応答の例を次に示します。

<?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>

そして、使用されるクラスは次のとおりです。

public class ErrorDialog
{
    XElement self;

    public ErrorDialog() { }

    public ErrorDialog(XElement errorDialog)
    {
        self = errorDialog;
    }

    public int errorDialogId
    {
        get { return (int)(self.Element("error_dialog_id")); }
    }
    public string errorDialogMessage
    {
        get { return (string)(self.Element("error_dialog_message")); }
    }
    public string errorDialogTitle
    {
        get { return (string)(self.Element("error_dialog_title")); }
    }
    public bool errorDialogIsSet
    {
        get { return (bool)(self.Element("error_dialog_is_set")); }
    }
    public List<ErrorDialogChoice> errorDialogChoice
    {
        get { return (List<ErrorDialogChoice>)(errorDialogChoice); }
    }
    public int errorDialogSelectedOption
    {
        get { return (int)(self.Element("error_dialog_selected_option")); }
    }
}

class ErrorDialogChoice
{
    XElement self;

    public ErrorDialogChoice() { }

    public ErrorDialogChoice(XElement errorDialogChoice)
    {
        self = errorDialogChoice;
    }

    public int errorDialogChoiceOptionId
    {
        get { return (int)(self.Element("error_dialog_choice_option_id")); }
    }
    public string errorDialogChoiceOptionTitle
    {
        get { return (string)(self.Element("error_dialog_choice_option_title")); }
    }
}

そして、これが私がそれを解析している方法です:

XElement response = XElement.Parse(data);  

ErrorDialog[] dialogs = response
    .Element("response_error_dialogs")
    .Element("error_dialog_list")
    .Elements("error_dialog_choice")
    .Select(e => new ErrorDialog(e))
    .ToArray();

ErrorDialogChoice[] edChoices = response
    .Element("response_error_dialogs")
    .Element("error_dialog_list")
    .Element("error_dialog_choice")
    .Element("error_dialog_choice_option_list")
    .Elements("error_dialog_choice_option")
    .Select(e => new ErrorDialogChoice(e))
    .ToArray();

したがって、この例では、最初のerror_dialog_choiceオブジェクトにはList3 つのオブジェクトが含まerror_dialog_choice_optionれ、2 番目のオブジェクトには 2 つのerror_dialog_choice_optionオブジェクトが含まれ、それ以上のオブジェクトは戻ってくる可能性があります。どんな助けでも大歓迎です。ありがとう。

4

2 に答える 2

1

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;
}

エラー ダイアログにはもっと多くの種類があると思いますが、<error_dialog_choice>これは考えられる種類の 1 つにすぎません。この場合、サブクラス化を使用して、サブクラスをXmlArrayItem属性とともにリストできます。


またはファイルからまたはを使用してクラス定義を生成することもできます。サンプルファイルからスキーマを推測することもできます。xsd.exesvcutil.exe.xsd.wsdlxsd.exe.xml

xsd.exe /?
svcutil.exe /?
于 2012-07-29T23:41:34.590 に答える
0

XmlSerializerフレームワークに組み込まれたを使用します。属性を使用して、クラスとそのプロパティに相当する xml をマップします。

例:

[XmlElement("Txn")]
public List<Transaction> Items { get; set; }

XSD.exe を使用して XSD を生成し、XSD の C# コードを生成できます。サンプルの response.xml ファイルで Visual Studio コマンド プロンプトを使用します。例えば:

c:>xsd 応答.xml

c:>xsd 応答.xsd /c /edb

于 2012-07-29T23:42:57.233 に答える