从xml字符串获取信息

时间:2014-06-07 10:01:04

标签: c# linq-to-xml

    private void button1_Click(object sender, EventArgs e)
    {

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.eve-central.com/api/quicklook?typeid=34&usesystem=30002053");
        myRequest.Method = "GET";
        WebResponse myResponse = myRequest.GetResponse();
        StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
        string result = sr.ReadToEnd();
        sr.Close();
        myResponse.Close();
        MessageBox.Show(result);
        IEnumerable<string> prices = from price in result.Descendants("order") select (string)price.Attribute("price");

    }

result.Descendants("order")显示错误

  

“类型'char'不能用作泛型类型中的类型参数'T'   或方法“

如何将buy_orders的所有价格转换为双数组?

1 个答案:

答案 0 :(得分:0)

您正在尝试将Linq to XML方法应用于字符串;它不会那样工作,你必须首先将XML解析为XDocument

private void button1_Click(object sender, EventArgs e)
{

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.eve-central.com/api/quicklook?typeid=34&usesystem=30002053");
    myRequest.Method = "GET";
    XDocument doc;
    using (WebResponse myResponse = myRequest.GetResponse())
    using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
    {
        string result = sr.ReadToEnd();
        MessageBox.Show(result);
        doc = XDocument.Parse(result);
    }
    IEnumerable<string> prices = from price in doc.Descendants("order") select (string)price.Attribute("price");

}