我有以下xml代码,并希望获取有价值的sheltertype" Loft"和" Condos"在庇护所下列出Xelement。
<Shelters>
<Shelter>
<ShelterType>Loft</ShelterType>
<Price>250/500</Price>
<Area>250/500</Area>
<ScreenDisplayText>$250,000 / $500,000</ScreenDisplayText>
</Shelter>
<Shelter>
<ShelterType>TownHouse</ShelterType>
<Price>250/500</Price>
<Area>250/500</Area>
<ScreenDisplayText>$250,000 / $500,000</ScreenDisplayText>
</Shelter>
<Shelter>
<ShelterType>Condos</ShelterType>
<Price>250/500</Price>
<Area>250/500</Area>
<ScreenDisplayText>$250,000 / $500,000</ScreenDisplayText>
</Shelter>
<Shelter>
<ShelterType>apartment</ShelterType>
<Price>250/500</Price>
<Area>250/500</Area>
<ScreenDisplayText>$250,000 / $500,000</ScreenDisplayText>
</Shelter>
---
</Shelters>
我尝试使用后代,我得到了空值。寻找带有XElement的Linq来处理它。请帮助。
答案 0 :(得分:0)
我相信这会给你想要的结果。这是来自此处修改为您的问题LINQ to read XML
的其他帖子的示例XDocument xdoc = XDocument.Load("Data.xml");
var lv1s = from lv1 in xdoc.Descendants("Shelter")
select new
{
ShelterType = lv1.Element("ShelterType").Value
};
foreach (var lv in lv1s)
{
Console.WriteLine(lv.ShelterType);
}
答案 1 :(得分:0)
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> sheltertype = doc.Descendants("Shelter")
.Where(x => (x.Element("ShelterType").Value == "Loft") || (x.Element("ShelterType").Value == "Condos")).ToList();
}
}
}