【问题标题】:How to write this nested Linq To Xml query如何编写这个嵌套的 Linq To Xml 查询
【发布时间】:2009-01-08 17:13:27
【问题描述】:

您好,我正在为医学研究编写应用程序 他们将输入 性别、年龄和其他一些将被计算为 ResultValue 的值

现在我有一个 XML 文件,其中包含有关结果的一些信息 年龄,性别和结果值,我想打印出测试结果的描述(如果先证者属于哪个组) 需要注意的一件事是我必须处理值范围,这意味着实际值介于低部分和高部分之间...... 我有三组... 好的,这是我的 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 to xml 查询会是什么样子

性别=“女性”

年龄=29

结果值=17

这个先证者肯定属于第 1 组,我想 打印出匹配的描述...

但我正在努力让这个工作......

我正在寻找 C# 中的解决方案... 任何帮助都会很棒!!!

【问题讨论】:

  • 您是在寻找 VB.Net LINQ to XML 查询还是 C# 查询。非常不同的答案。
  • 哦,对不起!我正在寻找一个 c# 解决方案...

标签: c# xml linq


【解决方案1】:

这样的?

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 的单亲。

结果值.低 结果值.高 年龄.低 年龄高 性别类型 结果.描述

其实可以把上面的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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多