ozsenegal
ozsenegal

Reputation: 4143

Query in LINQ to XML?

I have the follow XML File:

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

I try to retrieve it values using the follow query:

 XDocument questionarioXML = XDocument.Load(HttpContext.Current.Server.MapPath("~/xmlRelacaoesQuestionario.xml"));
    var questao = from q in questionarioXML.Descendants("Questionario").Descendants("Relacoes") where q.Attribute("Marca").Value == "SADIA"
                  select new { 
                      Tipo = q.Element("Tipo").Attribute("ID").Value,
                      V1 = q.Element("V1").Value,
                      V2 = q.Element("V2").Value,
                      V3 = q.Element("V3").Value,
                      V4 = q.Element("V4").Value
                  };

But the var questionario is always NULL?

Any ideias?

Upvotes: 0

Views: 93

Answers (2)

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68747

Seems like you want

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

Which I think can be made more readable and more efficient using

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 gets the first level element, your Tipo element was on the second level, inside Questao.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1504102

I suspect questao isn't null - I suspect that the query is throwing a NullReferenceException when you try to execute it.

That's because you're trying to get at the <Tipo> element, but you've only selected down to the Relacoes element. Then you're trying to get V1-V4 from the "current" element instead of from the Tipo.

I suspect you want:

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

That certainly works in my test app.

Upvotes: 1

Related Questions