XML duplicating the results reading

4

I have a XML :

<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>
            <telefones>
                <telefone>(11)3265-0449</telefone>
                <telefone>(11)3265-0540</telefone>
            </telefones>
        </consulado>
    </consulados>
</pais>
<pais>
    <nome-pais>Albânia</nome-pais>  
    <consulados>        
        <consulado>
            <nome-consulado>Consulado da República da Albânia</nome-consulado>                  
            <cep>01310-100</cep>
            <telefone>(11) 3283-3305</telefone>
        </consulado>
    </consulados>
</pais>

When I read the phones, it always doubles:

CodeC#:

XDocumentxmlDoc=XDocument.Load(XmlReader.Create(@"XML\consulados_pt.xml"));
 var result2 = (from p in xmlDoc.Descendants("pais") 
                let Nomepais = p.Element("nome-pais").Value
                from f in xmlDoc.Descendants("telefones")
                                .SelectMany(x=> x.Elements("telefone")
                                .Select(t=>t.Value))
                let telefone = f    
                where Nomepais == SSP.Consulado.pais                
                select new ListaConsulado.Consulado()
                    {
                        NomePais = Nomepais,
                        Telefone = "Tel: " + telefone
                    }
                ).ToList();

obs: I put this code let telefone = f just for testing!

  • I wanted to get the 2 phones, without duplicating the list
  • And there's another detail, when I open another list item, it brings those even phones:

<telefone>(11)3265-0449</telefone><telefone>(11)3265-0540</telefone>

XAMLemcasodedúvidas

<phone:LongListSelectorName="lstCons"
    HorizontalAlignment="Center" 
    VerticalAlignment="Top" 
    LayoutMode="List" 
    IsGroupingEnabled="False"
    Width="456" SelectionChanged="lstCons_SelectionChanged" >
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="#111" Background="Transparent" Margin="0, 10, 0, 0" BorderThickness="0,0,0,2">
                            <StackPanel  VerticalAlignment="Center" Orientation="Vertical"  >
                                <TextBlock Text="{Binding NomeConsulado}" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource PhoneTextTitle3Style}"/>
                                <TextBlock Text="{Binding Endereco}" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource PhoneTextSmallStyle}"/>
                                <TextBlock Text="{Binding Cep}" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource PhoneTextSmallStyle}"/>
                                <TextBlock Text="{Binding Telefone}" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource PhoneTextSmallStyle}"/>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
    
asked by anonymous 29.04.2014 / 17:02

1 answer

1

The problem is in the telefone = f.Element("telefone").Value assignment, because the Element method returns only one element, which in this case will be the first one found.

Instead, use the Elements method, and the LINQ method SelectMany to create the list with the phones:

var result2 = xmlDoc.Descendants("telefones")
    .SelectMany(
        x => x.Elements("telefone")
            .Select(t => t.Value))
    .ToList();

EDIT

In case you could use the following, to obtain each consulate, with the name of the corresponding country, and the respective telephone numbers:

var consuladosComTelefones = xmlDoc
    .Element("paises")
    .Elements("pais")
    .SelectMany(
        p => p.Element("consulados") == null ? null : p.Element("consulados")
            .Elements("consulado")
            .Select(c => new ListaConsulado.Consulado
            {
                NomePais = p.Element("nome-pais").Value,
                Telefone = c.Element("telefones") == null ? null : c.Element("telefones")
                    .Elements("telefone")
                    .Select(t => t.Value).ToList(),
            })).ToList();

Assuming the consulate class looks like this:

public class Consulado
{
    public string NomePais { get; set; }
    public List<string> Telefone { get; set; }
}
    
29.04.2014 / 17:26