 |
 |
 |
 |
|
Thursday, September 09, 2010
|
|
|
|
|
|
 |
ConsumingXML.zip
Purpose
We will look at how to stream xml file from URL and display to simple textbox. Description seems to be simple and trivial but doing it right take little work.
Covered Topics
-
How to create xml loader class for handling of loading xml asynchronously.
-
How to bubbling up the event to the parent notifying success or failure of xml loading process.
-
How to use delegate and event.
-
How to create, and use custom EventArgs.
ConsumingXML.zip
Purpose
We will look at how to stream xml file from URL and display to simple textbox. Description seems to be simple and trivial but doing it right take little work.
Covered Topics
-
How to create xml loader class for handling of loading xml asynchronously.
-
How to bubbling up the event to the parent notifying success or failure of xml loading process.
-
How to use delegate and event.
-
How to create, and use custom EventArgs.
|
|
 |
|
|
|
 |
| IMPORTANT CLIENT ACCESS POLICY
|
|
|
|
|
 |
In order for Silverlight 2 application to access any of the resources in different domain, say for example silverlight running in www.MyDomain.Com wants to consume www.YourDomain.com/yourfile.xml, you would need ClientAccessPolicy.xml file.
This is also true if the Silverlight 2 application trying to consume WCF or Web services.
Place ClientAccessPolicy.xml at the web site root.
For more details on how this work please read http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx.
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
|
In order for Silverlight 2 application to access any of the resources in different domain, say for example silverlight running in www.MyDomain.Com wants to consume www.YourDomain.com/yourfile.xml, you would need ClientAccessPolicy.xml file.
This is also true if the Silverlight 2 application trying to consume WCF or Web services.
Place ClientAccessPolicy.xml at the web site root.
For more details on how this work please read http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx.
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
|
|
|
 |
|
|
|
|
|
 |
Follow the instruction from Creating Silverlight 2 Project

App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page(e.InitParams);
}
|
Page.xaml.cs
<UserControl x:Class="ConsumingXML.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Margin="32,8,30,88"
Text="" TextWrapping="NoWrap"
x:Name="txtXml" HorizontalScrollBarVisibility="Visible"
AcceptsReturn="False" IsReadOnly="False"
VerticalScrollBarVisibility="Visible" />
<TextBox Height="48" Margin="32,0,30,18"
VerticalAlignment="Bottom" Text=""
TextWrapping="NoWrap" x:Name="txtError"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"/>
</Grid>
</UserControl>
|
Page.xaml.cs
using System.Collections.Generic;
using System.Windows.Controls;
namespace ConsumingXML
{
public partial class Page : UserControl
{
private XmlLoader _xmlLoader;
private string _url;
public Page(IDictionary<string, string> initParams)
{
InitializeComponent();
_url = initParams["Url"];
_xmlLoader = new XmlLoader();
_xmlLoader.OnXmlLoaded += new XmlLoader.XmlLoaded(Xml_Loaded);
_xmlLoader.LoadXml(_url);
}
private void Xml_Loaded(object sender, XmlLoadedEventArgs args)
{
try
{
if (args.IsXmlLoaded)
{
txtXml.Text = args.Xml;
}
else
{
txtXml.Text = args.ErrorMessage.StackTrace;
txtError.Text = "The faile to load xml from "+_url;
}
}
finally
{
_xmlLoader.OnXmlLoaded -= new XmlLoader.XmlLoaded(Xml_Loaded);
}
}
}
}
|
XmlLoadedEventArgs.cs
using System;
namespace ConsumingXML
{
public class XmlLoadedEventArgs : EventArgs
{
public bool IsXmlLoaded { get; set; }
public string Xml { get; set; }
public Exception ErrorMessage { get; set; }
public XmlLoadedEventArgs(bool loadCompleted, string xml)
{
this.IsXmlLoaded = loadCompleted;
this.Xml = xml;
this.ErrorMessage = null;
}
public XmlLoadedEventArgs(bool loadCompleted, Exception errorMessage)
{
this.IsXmlLoaded = loadCompleted;
this.ErrorMessage = errorMessage;
}
}
}
|
XmlLoader.cs
using System;
using System.IO;
using System.Net;
using System.Xml.Linq;
namespace ConsumingXML
{
public class XmlLoader
{
public delegate void XmlLoaded(object sender, XmlLoadedEventArgs a);
public event XmlLoaded OnXmlLoaded;
public XmlLoader()
{
}
public void LoadXml(string url)
{
if (!url.ToLower().Contains("http://"))
{
url = "http://" + url;
}
WebClient wc = new WebClient();
wc.OpenReadCompleted += WebClient_OpenReadCompleted;
wc.OpenReadAsync(new Uri(url));
}
private void WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
XmlLoadedEventArgs args = null;
try
{
if (e.Error == null)
{
XDocument doc = null;
using (Stream s = e.Result)
{
doc = XDocument.Load(s);
}
args = new XmlLoadedEventArgs(true, doc.ToString());
}
else
{
args = new XmlLoadedEventArgs(false, e.Error);
}
}
catch (Exception ex)
{
args = new XmlLoadedEventArgs(false, ex);
}
finally
{
OnXmlLoaded(this, args);
}
}
}
}
|
Demo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="ConsumingXML.Web.Demo" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="System.Web.Silverlight"
Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/ConsumingXML.xap"
MinimumVersion="2.0.31005.0"
Width="400px" Height="300px" />
<br /><br />
Enter URL to get XML:
<asp:TextBox ID="txtUrl" runat="server" Width="438px">
www.newagesolution.net/SiteMap.aspx
</asp:TextBox>
<br /><br />
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Get XML" />
</div>
</form>
</body>
</html>
|
Demo.aspx.cs
using System;
using System.Text;
namespace ConsumingXML.Web
{
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Xaml1.InitParameters = BuildSilverlightParams(null);
}
}
private string BuildSilverlightParams(string urlToGetXml)
{
if (string.IsNullOrEmpty(urlToGetXml))
{
urlToGetXml = "www.newagesolution.net/SiteMap.aspx";
}
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Url={0}", urlToGetXml);
return sb.ToString();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Xaml1.InitParameters = BuildSilverlightParams(txtUrl.Text);
}
}
}
|
Follow the instruction from Creating Silverlight 2 Project

App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page(e.InitParams);
}
|
Page.xaml.cs
<UserControl x:Class="ConsumingXML.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Margin="32,8,30,88"
Text="" TextWrapping="NoWrap"
x:Name="txtXml" HorizontalScrollBarVisibility="Visible"
AcceptsReturn="False" IsReadOnly="False"
VerticalScrollBarVisibility="Visible" />
<TextBox Height="48" Margin="32,0,30,18"
VerticalAlignment="Bottom" Text=""
TextWrapping="NoWrap" x:Name="txtError"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"/>
</Grid>
</UserControl>
|
Page.xaml.cs
using System.Collections.Generic;
using System.Windows.Controls;
namespace ConsumingXML
{
public partial class Page : UserControl
{
private XmlLoader _xmlLoader;
private string _url;
public Page(IDictionary<string, string> initParams)
{
InitializeComponent();
_url = initParams["Url"];
_xmlLoader = new XmlLoader();
_xmlLoader.OnXmlLoaded += new XmlLoader.XmlLoaded(Xml_Loaded);
_xmlLoader.LoadXml(_url);
}
private void Xml_Loaded(object sender, XmlLoadedEventArgs args)
{
try
{
if (args.IsXmlLoaded)
{
txtXml.Text = args.Xml;
}
else
{
txtXml.Text = args.ErrorMessage.StackTrace;
txtError.Text = "The faile to load xml from "+_url;
}
}
finally
{
_xmlLoader.OnXmlLoaded -= new XmlLoader.XmlLoaded(Xml_Loaded);
}
}
}
}
|
XmlLoadedEventArgs.cs
using System;
namespace ConsumingXML
{
public class XmlLoadedEventArgs : EventArgs
{
public bool IsXmlLoaded { get; set; }
public string Xml { get; set; }
public Exception ErrorMessage { get; set; }
public XmlLoadedEventArgs(bool loadCompleted, string xml)
{
this.IsXmlLoaded = loadCompleted;
this.Xml = xml;
this.ErrorMessage = null;
}
public XmlLoadedEventArgs(bool loadCompleted, Exception errorMessage)
{
this.IsXmlLoaded = loadCompleted;
this.ErrorMessage = errorMessage;
}
}
}
|
XmlLoader.cs
using System;
using System.IO;
using System.Net;
using System.Xml.Linq;
namespace ConsumingXML
{
public class XmlLoader
{
public delegate void XmlLoaded(object sender, XmlLoadedEventArgs a);
public event XmlLoaded OnXmlLoaded;
public XmlLoader()
{
}
public void LoadXml(string url)
{
if (!url.ToLower().Contains("http://"))
{
url = "http://" + url;
}
WebClient wc = new WebClient();
wc.OpenReadCompleted += WebClient_OpenReadCompleted;
wc.OpenReadAsync(new Uri(url));
}
private void WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
XmlLoadedEventArgs args = null;
try
{
if (e.Error == null)
{
XDocument doc = null;
using (Stream s = e.Result)
{
doc = XDocument.Load(s);
}
args = new XmlLoadedEventArgs(true, doc.ToString());
}
else
{
args = new XmlLoadedEventArgs(false, e.Error);
}
}
catch (Exception ex)
{
args = new XmlLoadedEventArgs(false, ex);
}
finally
{
OnXmlLoaded(this, args);
}
}
}
}
|
Demo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="ConsumingXML.Web.Demo" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="System.Web.Silverlight"
Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/ConsumingXML.xap"
MinimumVersion="2.0.31005.0"
Width="400px" Height="300px" />
<br /><br />
Enter URL to get XML:
<asp:TextBox ID="txtUrl" runat="server" Width="438px">
www.newagesolution.net/SiteMap.aspx
</asp:TextBox>
<br /><br />
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Get XML" />
</div>
</form>
</body>
</html>
|
Demo.aspx.cs
using System;
using System.Text;
namespace ConsumingXML.Web
{
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Xaml1.InitParameters = BuildSilverlightParams(null);
}
}
private string BuildSilverlightParams(string urlToGetXml)
{
if (string.IsNullOrEmpty(urlToGetXml))
{
urlToGetXml = "www.newagesolution.net/SiteMap.aspx";
}
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Url={0}", urlToGetXml);
return sb.ToString();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Xaml1.InitParameters = BuildSilverlightParams(txtUrl.Text);
}
}
}
|
|
|
 |
|
|
|
|
|
 |
|
|
|
 |
 |
 |
 |
|
|