如何写这个嵌套Linq到XML查询 [英] How to write this nested Linq To Xml query

查看:116
本文介绍了如何写这个嵌套Linq到XML查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在写一个应用程序的一个医学研究
他们将B $输入$ B的性别,年龄,哪些将被计算到ResultValue

Hi I am writing an app for a medical study they will type in the gender, the age, and some other values which will be calculated to a ResultValue

现在我有保存有关结果在年龄,性别和ResultValues组合
部分信息的XML文件,我想打印出的TestResult(的描述的情况下,在其中组先证者所属)
有一点要注意的是,我必须处理取值范围,这意味着实际值在低部分和高部分...
我有三个组之间...
OK票数是我的XML文件

now I have an XML file which holds some information about the result in combination of Age, Gender and the ResultValues and I would like to print out the description of the TestResult (in case in which group the proband belong) One thing to note is that I have to deal with value ranges this means the actual value lies between the low part and the high part... I have three groups... OK hier is my XML file

<?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 to XML查询一样,如果我有

What would look my linq to xml query like if I have

性别=女

年龄= 29

ResultValue = 17

ResultValue=17

这先证者肯定将属于第1组,我想
打印出匹配的说明...

this proband would certainly belong to Group 1 and I would like to print out the matching Description...

但我撞我的头得到这个工作...

But I am banging my head to get this working...

我要寻找一个在C#中的解决方案......
任何帮助将是巨大的!

I am looking for a solution in c#... Any help would be great!!!

推荐答案

这样呢?

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。每个结果值都有年龄的单亲,性别的单亲,结果的单亲家庭。

The idea is that you can imagine a row-column shape where each row is a ResultValue. Each result value has a single parent of Age, a single parent of Gender, an a single parent of Result.

ResultValue.Low
ResultValue.High
Age.Low
Age.High
Gender.Type
Result.Description

ResultValue.Low ResultValue.High Age.Low Age.High Gender.Type Result.Description

在事实上,人们可以预测上面的XML成形状:

In fact, one can project the above xml into that shape:

        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);

这篇关于如何写这个嵌套Linq到XML查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆