Accessing XML Data with an XmlReader : Xml Data « Data « Silverlight






Accessing XML Data with an XmlReader

Accessing XML Data with an XmlReader
   
<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
  <StackPanel>
    <Button Height="27.1" HorizontalAlignment="Left" Margin="8,8,0,7.5" VerticalAlignment="Stretch" Content="Retrieve XML" d:LayoutOverrides="Height" x:Name="ButtonReadXML" Click="ButtonReadXML_Click" Width="106"/>
    <ListBox Margin="8" x:Name="XmlData"/>
  </StackPanel>
</UserControl>

//File: Page.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Xml;

namespace SilverlightApplication3
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private void ButtonReadXML_Click(object sender, RoutedEventArgs e)
    {

      XmlReaderSettings XmlRdrSettings = new XmlReaderSettings();
      XmlRdrSettings.XmlResolver = new XmlXapResolver();
      XmlReader reader = XmlReader.Create("Books.xml", XmlRdrSettings);

      reader.MoveToContent();

      while (!reader.EOF)
      {
        reader.ReadToFollowing("Book");
        XmlData.Items.Add(reader.ReadInnerXml());
      }
      reader.Close();
    }
  }
}
/*
<Books xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Book>
    <ID>1</ID>
    <ISBN>1</ISBN>
    <Author>A</Author>
    <Title>C</Title>
    <Description></Description>
    <DatePublished>2001</DatePublished>
    <NumPages>640</NumPages>
    <Price>$59.99</Price>
  </Book>
</Books>
*/

   
    
    
  








Related examples in the same category

1.Using XmlReader and XmlWriter
2.Serializing a Data Object into XML
3.Read xmlRead xml