Error reading XML

2

When I try to read xml, return it to me: {System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Xml.Linq.XElement,SSP.Class.ListaConsulado>}

XML:

<?xmlversion="1.0" encoding="UTF-8"?>
<paises>
    <pais>
        <nome-pais>África do Sul</nome-pais>
        <consulados>        
            <consulado>
                <nome-consulado>Consulado da República da África do Sul</nome-consulado>        
                <endereco>Av. Paulista 1754, 12º andar</endereco>       
                <cep>01310-100</cep>
                <telefone>(11)3265-0449</telefone>
                <fax>(11) 3285-1185</fax>
                <geral>1</geral>
                <honorario>0</honorario>
                <nome_img_bandeira>flag_africa_sul</nome_img_bandeira>
            </consulado>
        </consulados>
    </pais>
</paises>

CLASSE

namespace SSP.Class
{
    class ListaConsulado
    {
        /*public string paises { get; set; }
        public string pais { get; set; }
        public string nome_pais { get; set; }
        public string consulados { get; set; }
        public string consulado { get; set; }
        public string nome_consulado { get; set; }*/
        public string endereco { get; set; }
        /*public string cep { get; set; }
        public string telefone { get; set; }
        public string fax { get; set; }
        public string geral { get; set; }
        public string honorario { get; set; }
        public string nome_img_bandeira { get; set; }*/
    }
}

Note: I just left the address to test

Código C#

var element = XElement.Load(@"XML\consulados_pt.xml");
var paises = from var in element.Descendants("paises")
    //orderby var.Element("nome-pais").Value
    select new ListaConsulado 
    {
        endereco = var.Element("endereco").Value
    };
lstConsPais.DataContext = paises;

XAML

<phone:LongListSelector Name="lstConsPais" HorizontalAlignment="Left" VerticalAlignment="Top" LayoutMode="List" IsGroupingEnabled="False" Width="446">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding endereco}" />
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
    
asked by anonymous 25.04.2014 / 21:29

1 answer

1

Solution

To read the data from this XML , at Windows Phone would have this form,

I suggest this class layout according to what I saw in xml, where countries can have more than one consulate.

public class Paises
{
    public Paises()
    {
        this.NomePais = string.Empty;
        Consulados = new List<Consulado>();
    }
    public Paises(String NomePais)
    {
        this.NomePais = NomePais;
        Consulados = new List<Consulado>();
    }
    public Paises(String NomePais, List<Consulado> Consulados)
    {
        this.NomePais = NomePais;
        this.Consulados = Consulados;
    }

    public Paises(List<Consulado> Consulados)
    {
        this.NomePais = string.Empty;
        this.Consulados = Consulados;
    }
    public String NomePais { get; set; }
    public IList<Consulado> Consulados { get; set; }
}

public class Consulado
{
    public string NomeConsulado { get; set; }
    public string Endereco { get; set; }
    public string Cep { get; set; }
    public string Telefone { get; set; }
    public string Fax { get; set; }
    public string Geral { get; set; }
    public string Honorario { get; set; }
    public string NomeImgBandeira { get; set; }
}

Read and convert to class with XmlDocument :

XDocument xmlDoc = XDocument.Load(XmlReader.Create(@"XML\consulados_pt.xml"));
var result1 = (from p in xmlDoc.Descendants("pais")
            let Nomepais = p.Element("nome-pais").Value
            select new
            {
                Nomepais
            })
            .FirstOrDefault();

var result2 = (from p in xmlDoc.Descendants("consulado")
                   let nomeconsulado = p.Element("nome-consulado").Value
                   let endereco = p.Element("endereco").Value
                   let cep = p.Element("cep").Value
                   let telefone = p.Element("telefone").Value
                   let geral = p.Element("geral").Value
                   let honorario = p.Element("honorario").Value
                   let nomeimgbandeira = p.Element("nome_img_bandeira").Value
                   let fax = p.Element("fax").Value
                   select new Consulado()
                   {
                       Cep = cep,
                       Endereco = endereco,
                       Fax = fax,
                       Geral = geral,
                       Honorario = honorario,
                       NomeConsulado = nomeconsulado,
                       NomeImgBandeira = nomeimgbandeira,
                       Telefone = telefone
                   }).ToList();

Paises pais = new Paises(result1.Nomepais, result2);

Another thing I noticed is that you're trying to load a LongListSelector :

<phone:LongListSelector Name="lstConsPais"
    HorizontalAlignment="Left" 
    VerticalAlignment="Top" 
    LayoutMode="List" 
    IsGroupingEnabled="False"
    Width="446" 
    SelectionChanged="lstConsPais_SelectionChanged" 
    Height="391" Margin="0,182,0,0">
<phone:LongListSelector.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBlock Text="{Binding Endereco}" />
        </StackPanel>
    </DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

The correct thing would be to pass the list on ItemsSource :

lstConsPais.ItemsSource = result2;

Result

    
26.04.2014 / 17:52