从XML字符串获取信息

本文关键字:信息 获取 字符串 XML | 更新日期: 2023-09-27 18:07:06

    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中的所有价格放入双数组中?

从XML字符串获取信息

您正在尝试将Linq到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");
}