2010-11-09 109 views
0

我有如下的XML文件:在LINQ to 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> 

我嘗試使用後續查詢檢索它的值:

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

但VAR questionario始終是NULL?

任何意識?

+0

哪個變量爲空?你不*有一個變量「問題」。你確定XDocument.Load調用正在工作嗎?如果這不起作用,那麼查詢本身就無關緊要。 – 2010-11-09 19:46:05

+0

questao is null,sorry – ozsenegal 2010-11-09 19:46:52

回答

1

好像你要

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

+0

幹得好!Thk ... – ozsenegal 2010-11-09 19:52:13

+0

我需要一些幫助,這個查詢只返回一個標籤Questao,我需要檢索所有的!!!請!!! – ozsenegal 2010-11-09 20:43:34

+0

@ user257234:你試過我的回答嗎?我相信它已經做到了你想要的。 – 2010-11-09 20:47:49

1

我懷疑questao不爲空 - 我懷疑查詢在嘗試執行時拋出NullReferenceException

這是因爲您試圖獲得<Tipo>元素,但您只選擇了Relacoes元素。然後你試圖從「當前」元素而不是從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 
    }; 

這當然在我的測試應用程序的工作原理。

+0

你是對的.. – ozsenegal 2010-11-09 19:53:54