2015-08-27 76 views
0

我嘗試通過Linq To XML將XML文件中的鏈接數據添加到我的WPF接口。下面,返回的錯誤和代碼:Linq XML問題和數據綁定

The type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'.

我的代碼

C#:

IEnumerable<string> datas = from d in XDocument.Load(@"Resources\limits.xml").Descendants("limits") 
            where (int)d.Element("ID") == id 
            select d.Elements; 

XAML/WPF

<StackPanel> 
    <TextBlock Text="{Binding XPath=explicationTitle}" FontWeight="Bold" Margin="10" /> 
    <TextBlock Text="{Binding XPath=explicationDescription}" Margin="10" /> 
    <TextBlock Text="Aucune" FontWeight="Bold" Margin="10" /> 
    <TextBlock Text="{Binding XPath=explicationLimiteAucune}" Margin="10" /> 
    <TextBlock Text="Modérée" FontWeight="Bold" Margin="10" /> 
    <TextBlock Text="{Binding XPath=explicationLimiteModeree}" Margin="10" /> 
    <TextBlock Text="Totale" FontWeight="Bold" Margin="10" /> 
    <TextBlock Text="{Binding XPath=explicationLimiteTotale}" Margin="10" /> 
</StackPanel> 

XML數據文件:

<Limits xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Limit> 
    <Id>1</Id> 
    <title>Ma première limite :)</title> 
    <description>NFC West</description> 
    <explication> 
     information 
     de 
     bla blabla 
    </explication> 
    <explicationTitle>Poney</explicationTitle> 
    <explicationDescription>Description de la limitation</explicationDescription> 
    <explicationLimiteAucune>Aucune limite..tout va bien :)</explicationLimiteAucune> 
    <explicationLimiteModeree>Quelques soucis, rien de grave</explicationLimiteModeree> 
    <explicationLimiteTotale>Gros soucis :)</explicationLimiteTotale> 
    </Limit> 
    <Limit> 
    <Id>2</Id> 
    <title>Limitation 2</title> 
    <description>NFC West</description> 
    <explication> 
     information 2 
     de 2 
     bla blabla 2 2 2 
    </explication> 
    <explicationTitle>Poney</explicationTitle> 
    <explicationDescription>Description de la limitation</explicationDescription> 
    <explicationLimiteAucune>Aucune limite..tout va bien :)</explicationLimiteAucune> 
    <explicationLimiteModeree>Quelques soucis, rien de grave</explicationLimiteModeree> 
    <explicationLimiteTotale>Gros soucis :)</explicationLimiteTotale> 
    </Limit> 
</Limits> 

如何創建一個漂亮的LINQ到XML請求的數據綁定?

回答

0

代碼中幾乎沒有問題。 首先我想提及的是,xml區分大小寫,所以請更正下面的查詢,其他明智的你得到這個錯誤: 值不能爲空。 參數名:元素

變化.Descendants(「限制」),以.Descendants(「限制」)

您也可以只使用無功而不是IEnumerable的,你會得到所有元素爲節點

var datas = from d in XDocument.Load(@"Resources\limits.xml").Descendants("Limit") 
       where (int)d.Element("Id") == 1 
       select d.Elements(); 
0

的問題是,Elements是一種方法,你忘了括號那邊所以它應該是d.Elements()但你仍然會得到一個轉換錯誤,因爲d.Elements將返回IEnumerable<XElement>,但你試圖將其存儲在IEnumerable<String>

您可以使用匿名類型喜歡這個項目的元素: -

var datas = from d in XDocument.Load(@"Resources\limits.xml").Descendants("limits") 
      where (int)d.Element("ID") == id 
      select new 
       { 
        explicationTitle = (string)d.Element("explicationTitle"), 
        explicationDescription = (string)d.Element("explicationDescription"), 
        ..and so on 
       }; 

但我不知道,如果匿名類型適用於WPF網格(因爲我有沒有經驗),在這種情況下,你可以定義一個Type和項目它,而不是匿名類型,像這樣: -

public class Limit 
{ 
    public string explicationTitle { get; set; } 
    public string explicationDescription{ get; set; } 
} 

然後你就可以預測這類型: -

List<Limit> datas = (from d in XDocument.Load(@"Resources\limits.xml") 
             .Descendants("limits") 
       where (int)d.Element("ID") == id 
       select new 
        { 
        explicationTitle = (string)d.Element("explicationTitle"), 
        explicationDescription = (string)d.Element("explicationDescription"), 
        ..and so on 
        }).ToList();