简体   繁体   English

LINQ to XML - Where子句

[英]LINQ to XML - Where clause

I have an XML file with 2 types of information - Locations and job types which is determined by the SLvl value. 我有一个包含两种类型信息的XML文件 - 位置和作业类型,由SLvl值决定。 I wish to bind the SearchTxt value for these to 2 drop downs (one for locations, one for job types) to be used as filters on my page. 我希望将这些的SearchTxt值绑定到2个下拉列表(一个用于位置,一个用于作业类型),以用作我页面上的过滤器。

The problem is I cant quite get my where clause to filter on the SLvl value. 问题是我无法通过我的where子句来过滤SLvl值。 With the where clause in no results are returned. 使用where子句,不返回任何结果。 If I remove it the query does return all the text values. 如果我删除它,查询会返回所有文本值。

C# C#

using System.Xml.Linq;
using System.Linq;
.....

// Loading from file
XDocument loaded = XDocument.Load(@"http://[LINKREMOVED]/vacancies.aspx");

// Query the data
var q = (from c in loaded.Descendants("items")
         where c.Element("SLvl").ToString() == "0"
         select c.Element("SearchTxt").ToString()).Distinct();

// Populate drop down
foreach(string name in q)
{
    ddlLocation.Items.Add(new ListItem(name, name));
}

XML: XML:

<VacancyMatch>
  <items>
   <SearchID>60</SearchID>
   <SearchTxt>Scotland</SearchTxt>
   <ParentID>0</ParentID>
   <SearchCatID>1</SearchCatID>
   <SLvl>1</SLvl>
   <SubCat>1</SubCat>
  </items>
  <items>
   <SearchID>92</SearchID>
   <SearchTxt>Accounting</SearchTxt>
   <ParentID>60</ParentID>
   <SearchCatID>2</SearchCatID>
   <SLvl>2</SLvl>
   <SubCat>2</SubCat>
 </items>
 ... More items here
</VacancyMatch>

I guess the problem is that the data is at the same level? 我想问题是数据处于同一水平? Its my first time using LINQ to XML so any help is greatly appriciated. 这是我第一次使用LINQ to XML,因此任何帮助都非常适合。 Note: XML is provided by a third party so formatting is up to them. 注意:XML由第三方提供,因此格式化取决于它们。

Drop the .ToString() and use .Value property instead: 删除.ToString()并使用.Value属性:

var values = loaded.Descendants("items")
    .Where(i => i.Element("SLvl").Value == "0")
    .Select(i => i.Element("SearchTxt").Value)
    .Distinct();

Calling ToString() on XElement will return entire node as text. 在XElement上调用ToString()将以文本形式返回整个节点。 For example, if we change i.Element("SearchTxt").Value in the query above to i.Element("SearchTxt").ToString() it will produce strings such as: 例如,如果我们将i.Element("SearchTxt").Value上面的查询中的i.Element("SearchTxt").Value更改为i.Element("SearchTxt").ToString()它将生成如下字符串:

<SearchTxt>Accounting</SearchTxt>

Accessing Value property will extract node's inner text - "Accounting" in this case. 访问Value属性将提取节点的内部文本 - 在这种情况下为“Accounting”

This: 这个:

where c.Element("SLvl").ToString() == "0"

should be: 应该:

where c.Element("SLvl").Value == "0"

You can't get a value of an element with "ToString()" method, you need to read it's "Value" property instead. 您无法使用“ToString()”方法获取元素的值,而是需要读取它的“Value”属性。

Same goes for any other lines where you are trying to get a value of an element. 对于您尝试获取元素值的任何其他行也是如此。

Hope it helps. 希望能帮助到你。

I notice one problem, you use ToString() on the XElements which is not exactly what you want, I think :), to get the text content of a XElement use the Value property. 我注意到一个问题,你在XElements上使用ToString(),这不是你想要的,我认为:),获取XElement的文本内容使用Value属性。

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.value.aspx http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.value.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM