Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ到XML中的查询?_C#_Linq - Fatal编程技术网

C# LINQ到XML中的查询?

C# LINQ到XML中的查询?,c#,linq,C#,Linq,我有以下XML文件: <Questionario> <Relacoes Marca="SADIA"> <Questao> <IDEtapa> 1 </IDEtapa> <IDQuestao> 1 </IDQuestao> <Tipo ID="1">

我有以下XML文件:

<Questionario>
    <Relacoes Marca="SADIA">
        <Questao>
        <IDEtapa>
        1
        </IDEtapa>
        <IDQuestao>
        1
        </IDQuestao>
        <Tipo ID="1">
            <V1></V1>
            <V2></V2>
            <V3></V3>
            <V4></V4>
        </Tipo>
        </Questao>
        <Questao>
            <IDEtapa>
                1
            </IDEtapa>
            <IDQuestao>
                2
            </IDQuestao>
            <Tipo ID="1">
                <V1>Ruim</V1>
                <V2>Regular</V2>
                <V3>Bom</V3>
                <V4>Ótimo</V4>
            </Tipo>
        </Questao>
    </Relacoes>
</Questionario>
但是var问题总是空的

有什么想法吗?

似乎是你想要的

var questao = from q in questionarioXML.Descendants("Questionario").Descendants("Relacoes")
                where q.Attribute("Marca").Value == "SADIA"
                select new
                {
                    Tipo = q.Element("Questao").Element("Tipo").Attribute("ID").Value,
                    V1 = q.Element("Questao").Element("Tipo").Element("V1").Value,
                    V2 = q.Element("Questao").Element("Tipo").Element("V2").Value,
                    V3 = q.Element("Questao").Element("Tipo").Element("V3").Value,
                    V4 = q.Element("Questao").Element("Tipo").Element("V4").Value
                };
我认为可以让它更具可读性,使用起来更有效

var questao = (from q in questionarioXML.Descendants("Questionario").Descendants("Relacoes")
                where q.Attribute("Marca").Value == "SADIA"
                select q)
    .Select(q => q.Element("Questao").Element("Tipo"))
    .Select(t => new
                        {
                            Tipo = t.Attribute("ID").Value,
                            V1 = t.Element("V1").Value,
                            V2 = t.Element("V2").Value,
                            V3 = t.Element("V3").Value,
                            V4 = t.Element("V4").Value
                        });

XElement.Element
获取第一级元素,您的
Tipo
元素位于第二级,在
Questao
内,我怀疑
Questao
不是空的-我怀疑当您尝试执行它时,查询正在抛出
NullReferenceException

这是因为您试图访问
元素,但只选择了
relaces
元素。然后尝试从“当前”元素而不是Tipo获取V1-V4

我怀疑你想要:

var questao = 
    from q in questionarioXML.Descendants("Questionario")
                             .Descendants("Relacoes") 
    where q.Attribute("Marca").Value == "SADIA"
    from tipo in q.Elements("Questao").Elements("Tipo")
    select new { 
        Tipo = tipo.Attribute("ID").Value,
        V1 = tipo.Element("V1").Value,
        V2 = tipo.Element("V2").Value,
        V3 = tipo.Element("V3").Value,
        V4 = tipo.Element("V4").Value
    };

这在我的测试应用程序中确实有效。

哪个变量为空?您没有变量“questionario”。您确定XDocument.Load调用正在工作吗?如果这不起作用,那么查询本身是不相关的。我需要一些帮助,这个查询只返回一个标记Questao,我需要检索所有标记!!!请@用户257234:你试过我的答案了吗?我相信它已经满足了你的要求。
var questao = 
    from q in questionarioXML.Descendants("Questionario")
                             .Descendants("Relacoes") 
    where q.Attribute("Marca").Value == "SADIA"
    from tipo in q.Elements("Questao").Elements("Tipo")
    select new { 
        Tipo = tipo.Attribute("ID").Value,
        V1 = tipo.Element("V1").Value,
        V2 = tipo.Element("V2").Value,
        V3 = tipo.Element("V3").Value,
        V4 = tipo.Element("V4").Value
    };