0

I have to process XML data which passed from StreamReader on MVC application. I have tried to pass into XDocument.Load() but this way not works. Please give me hints how can i grab data from StreamReader and loop with my Model to generate item collection. You dont need to process all my property just single one property process will help me to get idea.

Controller:

using (var response = (HttpWebResponse)req.GetResponse())
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        string responseStr = new StreamReader(responseStream).ReadToEnd();
        responseStream.Close();

        var items = new List<EbayDataViewModel>();

        //XDocument xdoc = XDocument.Load(responseStr);

        // Since i am only interested in <item> collections within <searchResult>
        //var searchResultItems = xdoc.Descendants()
        //    .Where(x => x.Name.LocalName == "item");


        //foreach (var sri in searchResultItems)
        //{
        //    // Get all child xml elements
        //    var childElements = sri.Elements();

        //    var itemId = childElements.FirstOrDefault(x => x.Name.LocalName == "itemId");
        //    var imageurl = childElements.FirstOrDefault(x => x.Name.LocalName == "galleryURL");
        //    var title = childElements.FirstOrDefault(x => x.Name.LocalName == "title");
        //    var url = childElements.FirstOrDefault(x => x.Name.LocalName == "viewItemURL");
        //    var locate = childElements.FirstOrDefault(x => x.Name.LocalName == "location");


        //    var nofwatch = childElements.FirstOrDefault(x => x.Name.LocalName == "listingInfo").Elements().FirstOrDefault(x => x.Name.LocalName == "watchCount");
        //    var price = childElements.FirstOrDefault(x => x.Name.LocalName == "sellingStatus").Elements().FirstOrDefault(x => x.Name.LocalName == "convertedCurrentPrice");

        //    //add items from xml data to EbayDataViewModel object
        //    items.Add(new EbayDataViewModel
        //    {
        //        ItemId = itemId == null ? String.Empty : itemId.Value,
        //        EbayImageUrl = imageurl == null ? String.Empty : imageurl.Value,
        //        EbayTitle = title == null ? String.Empty : title.Value,
        //        EbayUrl = url == null ? String.Empty : url.Value,
        //        NumberOfWatch = nofwatch == null ? String.Empty : nofwatch.Value,
        //        Location = locate == null ? String.Empty : locate.Value,
        //        EbayPrice = price == null ? String.Empty : price.Value,
        //    });

        //    var foo = items;

        //}

My Model:

public class EbayDataViewModel
{
    public string ItemId { get; set; }
    public string Seller { get; set; }
    public string EbayImageUrl { get; set; }
    public string EbayTitle { get; set; }
    public string EbayUrl { get; set; }
    public string EbayPrice { get; set; }
    public string Feedback { get; set; }
    public string NumberOfSales { get; set; }
    public string NumberOfWatch { get; set; }
    public string Location { get; set; }
}

Debug Picture Debug Picture XML Picture XML Picture

Waldi
  • 39,242
  • 6
  • 30
  • 78
John Lk
  • 185
  • 2
  • 15
  • See: https://stackoverflow.com/a/4842065/880990. Either use `Load(responseReader);` or `LoadXml(reaponseString)`. If you do the first don't call `.ReadToEnd()`. – Olivier Jacot-Descombes Oct 29 '17 at 12:45
  • Can you show me implementation of that into my code? – John Lk Oct 29 '17 at 12:59
  • You are calling `Load` with a string instead of the reader. If you correct it, does it still not work? Why does it not work? I would give you the advice to debug your code line by line, to inspect the variables and you will see why it does not work and what yo can do about it. – Olivier Jacot-Descombes Oct 29 '17 at 13:08
  • Suppose i have ready response in variable called "responseStr" what next? how can i grab value to loop with my models from there? – John Lk Oct 29 '17 at 13:11
  • I don't see you use the `InnerText` property: see: https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c – Olivier Jacot-Descombes Oct 29 '17 at 13:19
  • `Load(string)` does not expect XML-text but a file name! Delete this line: `string responseStr = new StreamReader(responseStream).ReadToEnd();` and call `XDocument.Load(responseStream)` (with parameter `responseStream` NOT `responseStr`). See: https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx – Olivier Jacot-Descombes Oct 29 '17 at 13:51

0 Answers0