我试图找出一个从两个不同元素派生的多个where子句。基本上,我希望能够根据DataType&的名称属性进行过滤。服务要素。感谢任何反馈。谢谢Jay
var services = from dt in doc.Descendants("DataType")
where (string)dt.Attribute("name") == "WELL_INDUSTRY" && (string)dt.Elements("Service").Attributes == "Well_Industry"
from service in dt.Elements("Services").Elements("Service").Elements("Layers").Elements("Layer")
select new
{
Name = (string)service.Attribute("name"),
};
XML:
<DataTypes>
<DataType name="WELL_INDUSTRY">
<Spatial>
<Services>
<Service name="Well_Industry" group="Well" status="Primary" >
<Layers>
<layer name="Bottom Hole Wells" ></layer>
<layer name="Bottom Hole Wells2" ></layer>
</Layers>
答案 0 :(得分:0)
我认为你正在寻找类似的东西:
var services = from dt in doc.Descendants("DataType")
where (string)dt.Attribute("name") == "WELL_INDUSTRY"
from service in dt.Element("Spatial")
.Elements("Services")
.Elements("Service")
where (string)service.Attribute("name") == "Well_Industry"
from layer in service.Element("Layers")
.Elements("Layer")
select new
{
ServiceName = (string)service.Attribute("name"),
Layers = layer.Select(x => (string)x).ToList()
};
where
之后添加另一个from service ...
。service
部分内使用select
变量。但是,在ServiceName
检查之前查询ServiceName == "myName"
似乎没用。如果您需要图层名称,请使用以下select
:
select new
{
Name = (string)layes.Attribute("name")
};
答案 1 :(得分:0)
var services = from dt in doc.Descendants("DataType")
where (string)dt.Attribute("name") == "WELL_INDUSTRY"
from s in dt.Descendants("Service")
where (string)s.Attribute("name") == "Well_Industry"
from l in s.Descendants("Layer")
select new {
Name = (string)l.Attribute("name")
};
使用XPath可以实现同样的目标:
var xpath = "//DataType[@name='WELL_INDUSTRY']//Service[@name='Well_Industry']//layer";
var services = from l in doc.XPathSelectElements(xpath)
select new {
Name = (string)l.Attribute("name")
};