none
Ler XML RRS feed

  • Pergunta

  • Bom pessoal e o sequinte tenho um XML com essa estrutura

     

    <carlist>
      <car>
        <carname>AC</carname>
        <carmodellist>
          <carmodel>Cobra</carmodel>
        </carmodellist>
      </car>
      <car>
        <carname>Acura</carname>
        <carmodellist>
          <carmodel>1.6 EL</carmodel>
          <carmodel>1.7 EL</carmodel>
          <carmodel>2.2 CL</carmodel>
          <carmodel>2.3 CL</carmodel>
          <carmodel>2.5 TL</carmodel>
          <carmodel>3.0 CL</carmodel>
          <carmodel>3.2 CL</carmodel>
          <carmodel>3.2 TL</carmodel>
          <carmodel>3.5 RL</carmodel>
          <carmodel>CL</carmodel>
          <carmodel>CSX</carmodel>
          <carmodel>EL</carmodel>
          <carmodel>Integra</carmodel>
          <carmodel>Legend</carmodel>
          <carmodel>MDX</carmodel>
          <carmodel>NSX</carmodel>
          <carmodel>NSX-T</carmodel>
          <carmodel>RDX</carmodel>
          <carmodel>RL</carmodel>
          <carmodel>RSX</carmodel>
          <carmodel>SLX</carmodel>
          <carmodel>TL</carmodel>
          <carmodel>TSX</carmodel>
          <carmodel>Vigor</carmodel>
        </carmodellist>
      </car>
      <car>
        <carname>Alfa Romeo</carname>
        <carmodellist>
          <carmodel>145</carmodel>
          <carmodel>146</carmodel>
          <carmodel>147</carmodel>
          <carmodel>155</carmodel>
          <carmodel>156</carmodel>
          <carmodel>159</carmodel>
          <carmodel>164</carmodel>
          <carmodel>166</carmodel>
          <carmodel>33</carmodel>
          <carmodel>75</carmodel>
          <carmodel>Alfetta</carmodel>
          <carmodel>GT</carmodel>
          <carmodel>GTV</carmodel>
          <carmodel>GTV-6</carmodel>
          <carmodel>Guiletta</carmodel>
          <carmodel>Milano</carmodel>
          <carmodel>Spider</carmodel>
        </carmodellist>
      </car>
      <car>

     <carname>Yugo</carname>
        <carmodellist>
          <carmodel>55</carmodel>
          <carmodel>Cabrio</carmodel>
          <carmodel>GV</carmodel>
        </carmodellist>
      </car>
    </carlist>

     

    como faço para pegar os Nodes <carname> <carmodel>

    nunca utilizei o XML e estou meio perdido!

    se alguem precisar eu tenha esse arquivo completo! de marcas e modelos!

    obrigado


    Charles Tenorio
    segunda-feira, 9 de janeiro de 2012 10:32

Respostas

  • Charles eu particulamente gosto de ler XML usando LINQ to XML, existe outras maneiras que seria com as classes XmlDocument ou XMlTextReader, segue abaixo um exemplo para LINQ to XML usando a classe XDocument:

    //Simulando XML
    string xml = @"<carlist>
                      <car>
                        <carname>AC</carname>
                        <carmodellist>
                          <carmodel>Cobra</carmodel>
                        </carmodellist>
                      </car>
                      <car>
                        <carname>Acura</carname>
                        <carmodellist>
                          <carmodel>1.6 EL</carmodel>
                          <carmodel>1.7 EL</carmodel>
                          <carmodel>2.2 CL</carmodel>
                          <carmodel>2.3 CL</carmodel>
                          <carmodel>2.5 TL</carmodel>
                          <carmodel>3.0 CL</carmodel>
                          <carmodel>3.2 CL</carmodel>
                          <carmodel>3.2 TL</carmodel>
                          <carmodel>3.5 RL</carmodel>
                          <carmodel>CL</carmodel>
                          <carmodel>CSX</carmodel>
                          <carmodel>EL</carmodel>
                          <carmodel>Integra</carmodel>
                          <carmodel>Legend</carmodel>
                          <carmodel>MDX</carmodel>
                          <carmodel>NSX</carmodel>
                          <carmodel>NSX-T</carmodel>
                          <carmodel>RDX</carmodel>
                          <carmodel>RL</carmodel>
                          <carmodel>RSX</carmodel>
                          <carmodel>SLX</carmodel>
                          <carmodel>TL</carmodel>
                          <carmodel>TSX</carmodel>
                          <carmodel>Vigor</carmodel>
                        </carmodellist>
                      </car>
                      <car>
                        <carname>Alfa Romeo</carname>
                        <carmodellist>
                          <carmodel>145</carmodel>
                          <carmodel>146</carmodel>
                          <carmodel>147</carmodel>
                          <carmodel>155</carmodel>
                          <carmodel>156</carmodel>
                          <carmodel>159</carmodel>
                          <carmodel>164</carmodel>
                          <carmodel>166</carmodel>
                          <carmodel>33</carmodel>
                          <carmodel>75</carmodel>
                          <carmodel>Alfetta</carmodel>
                          <carmodel>GT</carmodel>
                          <carmodel>GTV</carmodel>
                          <carmodel>GTV-6</carmodel>
                          <carmodel>Guiletta</carmodel>
                          <carmodel>Milano</carmodel>
                          <carmodel>Spider</carmodel>
                        </carmodellist>
                      </car>
                      <car>
                        <carname>Yugo</carname>
                        <carmodellist>
                          <carmodel>55</carmodel>
                          <carmodel>Cabrio</carmodel>
                          <carmodel>GV</carmodel>
                        </carmodellist>
                      </car>
                    </carlist>";
    
    
    //Usando método .Pars para transformar uma string em XML
    //Caso queira utilizar um arquivo XML use o método .Load()
    //var doc = System.Xml.Linq.XDocument.Load(@"C:\Arquivo.xml");
    var xDoc = System.Xml.Linq.XDocument.Parse(xml);
    
    //Recuperando apenas nó que seja "car" e depois lendo o carname e recuperando uma lista de carmodel do tipo string
    var resultado = from x in xDoc.Descendants("car")
                    select new
                    {
                        CarName = x.Element("carname").Value,
                        CarModelList = (from c in x.Descendants("carmodel")
                                       select c.Value).ToList()
                    };
    

    Resultado:
     


    Vitor Mendes | Seu feedback é muito importante para todos!
    Visite o meu site: http://www.vitormendes.com.br/
    segunda-feira, 9 de janeiro de 2012 11:50