Linq To XML 问题和数据绑定

Linq To XML problems and databinding

我尝试通过 Linq To XML 从 XML 文件到我的 WPF 界面的链接数据。下面,错误返回和代码:

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 请求创建和数据绑定?

问题是 Elements 是一个方法,你忘记了那里的括号所以它应该是 d.Elements() 但你仍然会得到一个转换错误,因为 d.Elements 将 return 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();

您的代码中几乎没有问题。 首先我想提一下 xml 是区分大小写的,所以请像下面一样更正您的查询,否则您会收到此错误: 值不能为空。 参数名称:元素

将 .Descendants("limits") 更改为 .Descendants("Limit")

您也可以只使用 var 而不是 IEnumerable,您将获得该节点的所有元素

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