Silverlight Resources & XML PRO

The Silverlight application is actually a package of files archived using ZIP and stored as a single file with the .xap extension. This file can include resources we want to be available for our application. We can alternatively keep these resources on the server or have them as part of the DLL file, which is the default behavior.

The following code sample includes an XML resource file packed as part of the DLL file. This code sample shows how simple it is to access that XML resource and how simple it is to parser its data using Linq to XML.

The following is the XML file (lib.xml). We save it within the root directory of our Silverlight application.

<?xml version="1.0" encoding="utf-8" ?>
<library>
  <book>
    <title>Core Silverlight</title>
    <author>Jonathan Taylor</author>
    <isbn>12312321231</isbn>
  </book>
  <book>
    <title>Core Python</title>
    <author>Greg Johnston</author>
    <isbn>42434234343</isbn>
  </book>
  <book>
    <title>F# Professional</title>
    <author>Nathan Shultz</author>
    <isbn>78723234233</isbn>
  </book>
</library>

The following is the XAML file that defines the user interface (MainPage.xaml).

<UserControl x:Class="SilverlightApplication21.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:DesignHeight="300" d:DesignWidth="800">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Press Here" Height="23" 
                HorizontalAlignment="Left" Margin="53,39,0,0" 
                Name="PressHereButton" VerticalAlignment="Top" 
                Width="75" Click="PressHereButtonClick" />
        <TextBox Height="36" HorizontalAlignment="Left" 
                 Margin="54,86,0,0" Name="DataTextBox" 
                 VerticalAlignment="Top" Width="691" />
    </Grid>
</UserControl>

The following is the code behind of the XAML file (MainPage.xaml.cs).

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace SilverlightApplication21
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private void PressHereButtonClick(object sender, RoutedEventArgs e)
        {
            var sri = Application.GetResourceStream(
                new Uri("lib.xml", UriKind.Relative));
            var stream = new StreamReader(sri.Stream);
            var str = stream.ReadToEnd();
            var ob = XDocument.Parse(str);
            var books = from x in ob.Descendants("book")
                        select new
                        {
                            Title = x.Descendants("title").First().Value,
                            Author = x.Descendants("author").First().Value,
                            Isbn = x.Descendants("isbn").First().Value
                        };
            foreach (var book in books)
            {
                DataTextBox.Text += book.Title + "\\" + book.Author + "   ";
            }
        }
    }
}

The following video clip explains this code sample and shows how does it run.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences