Skip to main content

Microsoft Silverlight

Answered Question load xml fileRSS Feed

(0)

mexican
mexican

Member

Member

164 points

713 Posts

load xml file

i have an xml file and want to load it into a listbox but nothing happens?

 <?xml version="1.0" encoding="utf-8" ?>
<Slides>
   <Slide>
     <Title>Slide 1</Title>
     <Text>Slide 1 Description</Text>
   </Slide>
   <Slide>
      <Title>Slide 2</Title>
      <Text>Slide 2 Description</Text>
  </Slide>
</Slides>

   Dim xmls As New XmlReaderSettings
        xmls.XmlResolver = New XmlXapResolver
        Dim reader As XmlReader = XmlReader.Create("x2.xml")

        reader.MoveToContent()

        While reader.Read
            If (reader.NodeType = XmlNodeType.Element) And (reader.Name = "slide") Then
                reader.ReadToFollowing("title")

                lb.Items.Add(reader.Value)
                reader.ReadToFollowing("text")

                lb.Items.Add(reader.Value)
            End If

            If (reader.NodeType = XmlNodeType.EndElement) And (reader.Name = "slides") Then
                Exit While
            End If
        End While

ken tucker
ken tucker

All-Star

All-Star

16344 points

2,498 Posts

Re: load xml file

I would databind the listbox to the data in the listbox

 

First add a ItemTemplate to the listbox
    <Grid x:Name="LayoutRoot" Background="White">
        <ListBox x:Name="listBox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Title}"></TextBlock>
                        <TextBlock Text="{Binding Text}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

 

 

Second create a class to hold the data

 

Public Class BindTo
    Private _Title As String
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property

    Private _Text As String
    Public Property Text() As String
        Get
            Return _Text
        End Get
        Set(ByVal value As String)
            _Text = value
        End Set
    End Property

End Class

Third use xml linq to get the data from the xml.  (Note you need to add a reference to system.xml.linq)

 

    Private Sub Page_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim x = <Slides>
                    <Slide>
                        <Title>Slide 1</Title>
                        <Text>Slide 1 Description</Text>
                    </Slide>
                    <Slide>
                        <Title>Slide 2</Title>
                        <Text>Slide 2 Description</Text>
                    </Slide>
                </Slides>

        Dim q = From p In x...<Slide> Select New BindTo With {.Title = x...<Title>.Value, .Text = x...<Text>.Value}

        listBox1.ItemsSource = q.ToList

    End Sub
 

amyo
amyo

Contributor

Contributor

3660 points

495 Posts

Re: load xml file

Thank Ken Tucker,

 

In C# it looks:

       public class Slide
       {
          public string Title {get;set; }
          public string Text{ get; set; }
       }

        public Page()
        {
            InitializeComponent();

             string slidesXml =
               "?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<Slides>" +
                    "Slide>" +
                        "<Title>Slide 1</Title>" +
                        "<Text>Slide 1 Description</Text>" +
                    "<Slide>" +
                    "<Slide>" +
                        "<Title>Slide 2</Title>" +
                        "<Text>Slide 2 Description</Text>" +
                    "<Slide>" +
                "<Slides>";
            XElement toLoad=XElement.Parse(slidesXml);
            var allSlides= toLoad.Elements().Select(slide => 
                new Slide 
                { 
                    Title = slide.Element("Title").Value, 
                    Text = slide.Element("Text").Value }
                 ).ToList();
            
            listBox1.ItemsSource = allSlides;
           
        }

Amyo Kabir
Solution Architect & Sr. Developer
Blog

mexican
mexican

Member

Member

164 points

713 Posts

Re: load xml file

 if i am loading xml from a file which is what i want then dont i need to load the file in somehow instead of hardcoding the xml?

ken tucker
ken tucker

All-Star

All-Star

16344 points

2,498 Posts

Re: load xml file

You could do something like this instead of hard coding the xml

 

Dim x = XDocument.Load("xmlfile1.xml")

mexican
mexican

Member

Member

164 points

713 Posts

Re: load xml file

 then i use the code as above after this line?

ken tucker
ken tucker

All-Star

All-Star

16344 points

2,498 Posts

Answered Question

Re: load xml file

Yes change

 

       Dim x = <Slides>
                    <Slide>
                        <Title>Slide 1</Title>
                        <Text>Slide 1 Description</Text>
                    </Slide>
                    <Slide>
                        <Title>Slide 2</Title>
                        <Text>Slide 2 Description</Text>
                    </Slide>
                </Slides>

To

 Dim x = XDocument.Load("x2.xml")

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities