Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Xml代码转换为linq_Xml_Linq - Fatal编程技术网

如何将Xml代码转换为linq

如何将Xml代码转换为linq,xml,linq,Xml,Linq,我正在做一个教程,效果很好。但我想知道我如何才能用linq做到这一点 我正在尝试这样的东西 XDocument doc = XDocument.Load("http://clients1.google.com/complete/search?hl=" + lang+ "&output=toolbar&q=" + word+ ""); var suggestions = doc.Descandands("suggestions").Where( .... 但我有点困惑,这是我的

我正在做一个教程,效果很好。但我想知道我如何才能用linq做到这一点

我正在尝试这样的东西

XDocument doc = XDocument.Load("http://clients1.google.com/complete/search?hl=" + lang+ "&output=toolbar&q=" + word+ "");

var suggestions = doc.Descandands("suggestions").Where( ....
但我有点困惑,这是我的xml代码

string lang = drpLang.SelectedValue;
    //TextBoxa girilen değer alınır.
    string word = txtSearch.Text;
    //Google Suggest Değelerini içeren Labelı ilk başta temizliyoruz.
    lblRetVal.Text = "";
    /*Burada Google Suggest değerlerini içeren servisin kaynak kodunu alıyoruz.
     * Xml olarak çekerken eşleşmeyen karakterler olduğu için böyle bir yol izliyoruz.
     * */
    WebRequest req = HttpWebRequest.Create("http://clients1.google.com/complete/search?hl=" + dil + "&output=toolbar&q=" + kelime + "");
    WebResponse response = req.GetResponse();
    StreamReader retValues = new StreamReader(response.GetResponseStream());
    string html = retValues.ReadToEnd();

    //XmlDocument xdoc = new XmlDocument();
    ////Tüm sayfayı çektiğimiz için sayfanın kaynak kodunu xml olarak yüklenmesi sağlıyoruz.
    //xdoc.LoadXml(html);
    //XmlNodeList nodeList = xdoc.SelectNodes("toplevel/CompleteSuggestion/suggestion");
    ////Burada alınan nodeları döngüye alıyoruz.
    //foreach (XmlNode item in nodeList)
    //{
    //    //Alınan nodeların data Attributes değerini tek tek labela yazdırıyoruz.
    //    lblRetVal.Text += item.Attributes["data"].InnerText + "<br>";
    //} 

我不确定我是否正确地理解了,但是如果您将使用LINQ查询XML数据,我建议您创建一个类,它包含所选后代中的所有XML元素,并考虑下面的示例代码。

首先,我创建一个包含所有要查询的xml元素的类

public class CompleteSuggestion
{
    public string suggestionData { get; set;}
}
这是我的linq到xml

public List<CompleteSuggestion> GetSuggestions(string lang, string word) 
{
    XDocument doc = XDocument.Load("http://clients1.google.com/complete/search?hl=" + lang+ "&output=toolbar&q=" + word+ "");
    List<CompleteSuggestion> suggestions = (from s in doc.Descendants("suggestion")
                                            select new CompleteSuggestion
                                            {
                                                suggestionData = s.Attribute("data").Value
                                            }).ToList();
    return suggestions;
}
最后,在需要的地方调用方法并使用它

List<CompleteSuggestion> suggestions = GetSuggestions("en", "angle");
foreach (CompleteSuggestion s in suggestions)
{
    Response.Write(s.suggestionData);
}