2

I'm working on a WP7 app which gets and updates data on a web server. If any updates need a response, I get a list of errors that needs to be dealt with, and a list of possible choices for each error. The issue I'm having is assigning each object its appropriate list of choices. As of now I get a list of errors, and another list of all possible choices for all errors. I'd like the error object to contain the list of only its options so I can handle that.

So here's an example response:

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

And here's the classes used:

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

And here's how I'm parsing it:

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();

So with this example, the first error_dialog_choice object will have a List containing 3 error_dialog_choice_option objects, the second has the two error_dialog_choice_option objects, and any more that may come back. Any help is appreciated. Thanks.

2
  • Might it be easier to use XmlSerializer? You could even use xsd.exe to generate the types from sample XML... Commented Jul 29, 2012 at 20:59
  • Agree with Marc use the XmlSerializer. Commented Jul 29, 2012 at 23:39

2 Answers 2

1

You can use XML serialization to achieve this much easier:

var reader = new StringReader(xmlString);
var ser = new XmlSerializer(typeof(Response));
var result = (Response) ser.Deserialize(reader);

Using these class definitions.

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

I am guessing there can be more types of error dialogs, and <error_dialog_choice> is just one of the possible types. In this case you could use subclassing, and list the subclasses with XmlArrayItem attributes.


You could also generate the class definitions with xsd.exe or svcutil.exe, from an .xsd or .wsdl file. xsd.exe can even infer the schema from a sample .xml file.

xsd.exe /?
svcutil.exe /?
Sign up to request clarification or add additional context in comments.

1 Comment

No you beat me by 1 minute! Very comprehensive answer though. :) I didn't go into all the detail.
0

Use the XmlSerializer built into the framework. Use attributes to map out the xml equivalent of your classes and their properties.

Example:

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

You can use the XSD.exe to generate an XSD and then generate C# code for your XSD. Use Visual Studio Command Prompt with a sample response.xml file. Eg:

c:>xsd response.xml

c:>xsd response.xsd /c /edb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.