如何打印从Web服务返回的响应

时间:2014-07-21 07:10:23

标签: c# xml web-services xml-parsing linq-to-xml

我是新手使用网络服务并尝试了解他们的结构,工作方式等等。我在网上找到了这个例子。其实我明白它的作用。我想改进这个例子。我想要做的是打印从此服务返回的所有值,但不知道该怎么做。任何指南或建议都会受到影响。

public void GetFeeds()
{
    WebClient wcXML = new WebClient();
    wcXML.OpenReadAsync(new Uri("http://cloud.tfl.gov.uk/TrackerNet/LineStatus"));
    wcXML.OpenReadCompleted += new OpenReadCompletedEventHandler(wClientUpdate);
}

/// <summary>        
/// Web Client Update Read Complete Event        
/// </summary>        
/// <param name="sender"></param>        
/// <param name="e"></param>        
void wClientUpdate(object sender, OpenReadCompletedEventArgs e)
{
    if (e.Error == null)
    {
        try
        {
            Stream Resultstream = e.Result;
            XmlReader reader = XmlReader.Create(Resultstream);
            XDocument xDocumentLive = XDocument.Load(reader);
            XNamespace ns = "http://webservices.lul.co.uk/";
            List<Feed> feedList = new List<Feed>();
            feedList.AddRange((from query in xDocumentLive.Element(ns + "ArrayOfLineStatus").Elements(ns + "LineStatus")
                           select new Feed
                           {
                               Name = (string)query.Element(ns + "Line").Attribute("Name").Value,
                               Description = (string)query.Element(ns + "Status").Attribute("Description").Value,
                               isActive = (string)query.Element(ns + "Status").Attribute("IsActive").Value
                           }).ToList());
        }
        catch (Exception ex)
        {
        }
    }

1 个答案:

答案 0 :(得分:0)

要将List<Feed>打印到Output Window,只需使用foreach进行迭代:

foreach (var feed in feedList)
{
   Debug.WriteLine("Name: {0}, Description: {1}, IsActive: {2}", feed.Name, feed.Description, feed.isActive)
}