Silverlight Save data in XML file at runtime in VB.NET

In this article you will learn the working of XML and Silvelight together for creating and saving data at runtime.
  • 2285
Download Files:
 

XML is an important medium in the world of Silverlight especially where Web Services are involved, the XML Reader and Writer classes that allow you high speed access to reading and writing XML documents, and to store the XML in the isolated storage, or in the server I just convert the XML into an string, quite standard param, only take care when using services to configure the params tu support more than 64 K's string parametrs (if you are in that case).

However, Silverlight 4 does have a couple of small features that might combine to do something interesting in this area, namely;

  • The ability to data-bind to string.
  • Indexers support for XPath.

and so I wondered whether I might be able to combine those two things together to do something "interesting" in terms of data-binding Silverlight UI to XML.

Example 

<UserControl x:Class="SilverlightApplication11.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="400"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="162,175,0,0"Name="button1" VerticalAlignment="Top" 

Width="75"Click="button1_Click" />
        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="27,39,0,0" Name="label1"VerticalAlignment="Top" 

Width="120" Content="First Name" />
        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="27,90,0,0" Name="label2"VerticalAlignment="Top" 

Width="120" Content="Last Name" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="209,39,0,0" Name="firstNameText"VerticalAlignment="Top"

 Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="209,86,0,0" Name="lastNameText"VerticalAlignment="Top"

 Width="120" />
    </Grid>
</
UserControl>

// MainPage.xaml.vb Code

Imports
 System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Xml.Linq
Imports System.IO
Namespace SilverlightApplication11
       Partial Public Class MainPage
              Inherits UserControl
              Public Sub New()
                     InitializeComponent()
              End Sub
              Private Function CreateXML() As XElement
                     Dim userInfo As New XElement("names")
                     userInfo.Add(New XElement("first", firstNameText.Text))
                     userInfo.Add(New XElement("last", lastNameText.Text)) 
                     Return userInfo 
              End Function
              Private Sub button1_Click(ByVal sender As ObjectByVal e As RoutedEventArgs)
                     Dim saveFileDialog_Renamed As New SaveFileDialog() 
                     saveFileDialog_Renamed.DefaultExt = "xml"
                     saveFileDialog_Renamed.Filter = "XML./ Files (*.xml)|*.xml|All files (*.*)|*.*"
                     saveFileDialog_Renamed.FilterIndex = 1
                     If saveFileDialog_Renamed.ShowDialog() = True Then
                           Using stream_Renamed As Stream = saveFileDialog_Renamed.OpenFile() 
                                  Dim sw As New StreamWriter(stream_Renamed, System.Text.Encoding.UTF8)
                                  sw.Write(CreateXML().ToString())
                                  sw.Close()
                                  stream_Renamed.Close()
                           End Using
                     End If
              End Sub
       End 
Class
End Namespace 

Output Window

1.gif
 

XMl file after updating records:

2.gif
 

Summary

I hope this article help you to understand  the working of XML and Silvelight together for creating and saving data at runtime.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.