您好我正在为医学研究编写应用程序 他们会输入 性别,年龄以及将计算到ResultValue的其他一些值
现在我有一个XML文件,其中包含有关结果的一些信息 Age,Gender和ResultValues,我想打印出TestResult的描述(如果先证者属于哪个组) 需要注意的一点是,我必须处理值范围,这意味着实际值位于低部分和高部分之间...... 我有三个小组...... 好的hier是我的XML文件
<?xml version="1.0" encoding="iso-8859-1"?>
<Result>
<ID>1</ID>
<Description>You belong to Group 1</Description>
<Genders>
<Gender type="female">
<Ages>
<Age low="18" high="24">
<ResultValue low="0" high="19"/>
</Age>
<Age low="25" high="34">
<ResultValue low="0" high="20"/>
</Age>
<Age low="35" high="44">
<ResultValue low="0" high="21"/>
</Age>
<Age low="45" high="54">
<ResultValue low="0" high="22"/>
</Age>
<Age low="55" high="64">
<ResultValue low="0" high="23"/>
</Age>
<Age low="65" high="110">
<ResultValue low="0" high="24"/>
</Age>
</Ages>
</Gender>
<Gender type="male">
<Ages>
<Age low="18" high="24">
<ResultValue low="0" high="19"/>
</Age>
<Age low="25" high="34">
<ResultValue low="0" high="20"/>
</Age>
<Age low="35" high="44">
<ResultValue low="0" high="21"/>
</Age>
<Age low="45" high="54">
<ResultValue low="0" high="22"/>
</Age>
<Age low="55" high="64">
<ResultValue low="0" high="23"/>
</Age>
<Age low="65" high="110">
<ResultValue low="0" high="24"/>
</Age>
</Ages>
</Gender>
</Genders>
</Result>
<Result>
<ID>2</ID>
<Description>You belong to Group 2</Description>
<Genders>
<Gender type="female">
<Ages>
<Age low="18" high="24">
<ResultValue low="19" high="24"/>
</Age>
<Age low="25" high="34">
<ResultValue low="20" high="25"/>
</Age>
<Age low="35" high="44">
<ResultValue low="21" high="26"/>
</Age>
<Age low="45" high="54">
<ResultValue low="22" high="27"/>
</Age>
<Age low="55" high="64">
<ResultValue low="23" high="28"/>
</Age>
<Age low="65" high="110">
<ResultValue low="24" high="29"/>
</Age>
</Ages>
</Gender>
<Gender type="male">
<Ages>
<Age low="18" high="24">
<ResultValue low="19" high="24"/>
</Age>
<Age low="25" high="34">
<ResultValue low="20" high="25"/>
</Age>
<Age low="35" high="44">
<ResultValue low="21" high="26"/>
</Age>
<Age low="45" high="54">
<ResultValue low="22" high="27"/>
</Age>
<Age low="55" high="64">
<ResultValue low="23" high="28"/>
</Age>
<Age low="65" high="110">
<ResultValue low="24" high="29"/>
</Age>
</Ages>
</Gender>
</Genders>
</Result>
<Result>
<ID>3</ID>
<Description>You belong to group 3</Description>
<Genders>
<Gender type="female">
<Ages>
<Age low="18" high="24">
<ResultValue low="24" high="29"/>
</Age>
<Age low="25" high="34">
<ResultValue low="25" high="30"/>
</Age>
<Age low="35" high="44">
<ResultValue low="26" high="31"/>
</Age>
<Age low="45" high="54">
<ResultValue low="27" high="32"/>
</Age>
<Age low="55" high="64">
<ResultValue low="28" high="33"/>
</Age>
<Age low="65" high="110">
<ResultValue low="29" high="34"/>
</Age>
</Ages>
</Gender>
<Gender type="male">
<Ages>
<Age low="18" high="24">
<ResultValue low="24" high="29"/>
</Age>
<Age low="25" high="34">
<ResultValue low="25" high="30"/>
</Age>
<Age low="35" high="44">
<ResultValue low="26" high="31"/>
</Age>
<Age low="45" high="54">
<ResultValue low="27" high="32"/>
</Age>
<Age low="55" high="64">
<ResultValue low="28" high="33"/>
</Age>
<Age low="65" high="110">
<ResultValue low="29" high="34"/>
</Age>
</Ages>
</Gender>
</Genders>
</Result>
如果我有
,我的linq到xml查询会是什么样的性别= “女”
年龄= 29
ResultValue = 17
这个先证者肯定属于第1组,我想 打印出匹配的描述......
但是我正在敲打我的头来让这个工作......
我正在寻找c#的解决方案...... 任何帮助都会很棒!!!
答案 0 :(得分:6)
这样的东西?
XElement myElement = XElement.Parse(xmlstring);
int resultValue = 17;
int age = 26;
string genderValue = "female";
IEnumerable<string> query =
myElement.Descendants("ResultValue")
.Where(rv => ((int)rv.Attribute("low")) <= resultValue)
.Where(rv => ((int)rv.Attribute("high")) >= resultValue)
.Where(rv => rv.Ancestors("Age")
.Any(a => ((int) a.Attribute("low")) <= age && ((int) a.Attribute("high")) >= age)
)
.Where(rv => ((string)rv.Ancestors("Gender").Single().Attribute("type")) == genderValue)
.Select(rv => rv.Ancestors("Result").Single().Element("Description").Value);
foreach (string x in query)
Console.WriteLine(x);
这个想法是你可以想象一个行列形状,其中每一行都是ResultValue。每个结果值都有一个Age的父级,Gender的单个父级,Result的单个父级。
ResultValue.Low ResultValue.High Age.Low Age.High Gender.Type Result.Description
实际上,可以将上面的xml投影到该形状中:
var query2 = myElement.Descendants("ResultValue")
.Select(rv => new
{
ResultValue = rv,
Age = rv.Ancestors("Age"),
Gender = rv.Ancestors("Gender"),
Result = rv.Ancestors("Result")
})
.Select(x => new XElement("Data",
new XAttribute("ResultValue.Low", (int)x.ResultValue.Attribute("low")),
new XAttribute("ResultValue.High", (int)x.ResultValue.Attribute("high")),
new XAttribute("Age.Low", (int)x.Age.Attributes("low").Single()),
new XAttribute("Age.High", (int)x.Age.Attributes("high").Single()),
new XAttribute("Gender.Type", (string) x.Gender.Attributes("type").Single()),
new XAttribute("Result.Description", (string) x.Result.Elements("Description").Single())
));
foreach (XElement x in query2)
Console.WriteLine(x);