从StreamReader获取数据并将其设置为模型

时间:2017-10-29 12:37:36

标签: c# asp.net-mvc

我必须处理从MVC应用程序上的StreamReader传递的XML数据。我试图传递到XDocument.Load()但这种方式不起作用。请给我提示如何从StreamReader中获取数据并使用我的模型循环以生成项目集合。你不需要处理我的所有财产只需一个属性过程将帮助我了解。

控制器:

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;

        //}

我的模特:

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 XML图片 XML Picture

0 个答案:

没有答案