John Lk
John Lk

Reputation: 185

Process XML output from API

I am getting an XML return from an Ebay API call. This is actually an Ebay category list of collections. But the problem is, I can't access its collection from XML output. I have attached two pictures - the first one showing debug of XML value returning variable, and the second one showing "InnerList". My main goal is prepare this XML data to store on my database, so I need a clean list of values from XML data. Any ideas?

Picture One

Picture Two

Upvotes: 0

Views: 114

Answers (2)

Troels Thisted
Troels Thisted

Reputation: 76

You could deserialize your xml into your own class/object - Then it might be easier to work with. All i do is put xml tags to a class and i can deserialize it. See the class and method below:

public static T Deserialize<T>(string xmlText)
{
    try
    {
        var stringReader = new System.IO.StringReader(xmlText);
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(stringReader);
    }
    catch
    {
        throw;
    }
}

[XmlElement("adress")]
public class Adress
{
    [XmlElementAttribute("street_address")]
    public string street_address { get; set; }

    [XmlElementAttribute("postal_code")]
    public string postal_code { get; set; }

    [XmlElementAttribute("city")]
    public string city { get; set; }

    [XmlElementAttribute("country")]
    public string country { get; set; }
}

public main()
{
     Adress myAdress = Deserialize<Adress>(XMLstring);
}

Hope it helps!

Upvotes: 1

Liakat Hossain
Liakat Hossain

Reputation: 1384

It seems you are using Ebay SDK. Please try code below to process return values.

foreach (CategoryTypeCollection item in categories)
                    {
                        item.ItemAt(0).CategoryID = "This is how you access the properties of he returned result";
                        // THE XML is already parsed for you via SDK, so you don't have to parse it... 
                        // since i wrote foreach loop here, always access itemAt 0th index posiiton 
                    }

Upvotes: 0

Related Questions