Silverlight读取XML代码(非常有用^-^)
来源:互联网 发布:json转数组 编辑:程序博客网 时间:2024/06/11 00:37
Reading XML with Silverlight
时间:2009-12-23 10:04来源:SilverlightChina.Net 作者:银光中国网 点击:369次
XML (Extensible Markup Language) is a great format for saving structured data in. In this Tip I will be showing you how to read and process XML files from Silverlight using the XmlReader object. Lets say, for example, you want to store a tree structure of
XML (Extensible Markup Language) is a great format for saving structured data in. In this Tip I will be showing you how to read and process XML files from Silverlight using the XmlReader object.
Let’s say, for example, you want to store a tree structure of images grouping by them category. Your XML could look like this:
<?xml version="1.0" encoding="utf-8" ?>
<ImageTree>
<Area name="grass">
<Image name="Normal Grass">grass1.png</Image>
<Image name="Dry Grass">grass2.png</Image>
<Image name="Mixed Grass">grass3.png</Image>
<Image name="Long Grass">grass4.png</Image>
</Area>
<Area name="tile">
<Image name="Brick">brick.png</Image>
<Image name="White Stone">stone.png</Image>
<Image name="Cracked Stone">crackedstone.png</Image>
<Image name="Black Brick">brick2.png</Image>
</Area>
</ImageTree>
Few things to note about the XML above:
- XML can only have one root node which in my case I have called <ImageTree>
- <Area> and <Image> tags are called Elements
- “name” is an attribute and its content is the value for the attribute.
Now, on to the code below:
- To open an XML file I will be using the WebClient object. I have placed a file called MapImages.xml in my ClientBin folder that contains the data I will read. When the file read operation is complete (remember everything is asynchronous) the callback function client_DownloadStringCompleted will be called.
- When reading the data I check to see what the NodeType is. I only care about nodes that are of type Element or Text and I ignore stuff like comments, whitespace, etc.
- Once you read an Element you can get the attribute (such as name=”Brick”) for the Element by calling reader.MoveToFirstAttribute().
- The file name for each image is stored in the Tag property of each tree view item.
public Page()
{
InitializeComponent();
Uri url = new Uri("MapImages.xml", UriKind.Relative);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
TreeViewItem areaItem = null;
TreeView tv = new TreeView();
TreeViewItem rootItem = new TreeViewItem();
rootItem.Header = "Images";
tv.Items.Add(rootItem);
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string imageName = String.Empty;
string areaName = String.Empty;
string fileName = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "Area")
{
if (true == reader.MoveToFirstAttribute())
{
areaName = reader.Value;
areaItem = new TreeViewItem();
areaItem.Header = areaName;
rootItem.Items.Add(areaItem);
}
}
else if (reader.Name == "Image")
{
if (true == reader.MoveToFirstAttribute())
{
imageName = reader.Value;
}
}
}
else if (reader.NodeType == XmlNodeType.Text)
{
fileName = reader.Value;
TreeViewItem imageItem = new TreeViewItem();
imageItem.Header = imageName;
imageItem.Tag = fileName;
if (null != areaItem)
areaItem.Items.Add(imageItem);
}
}
MainCanvas.Children.Add(tv); // Add the treeview to our main canvas.
}
}
The result of the code above is a tree view with all the items added as seen in the image below.
On a final note, this code is hard wired to read a specifically formatted and error free file. You will want to add error checking in case the file is invalid in anyway.
- Silverlight读取XML代码(非常有用^-^)
- 分享非常有用的Java程序 (关键代码)(六)---解析/读取XML 文件(重要)
- Silverlight读取*.XML数据
- 怎样用Silverlight读取xml
- SilverLight读取XML文件
- Silverlight读取xml
- Silverlight读取XML
- silverlight 读取XML 写入 删除
- silverlight 读取Xml 写入 删除
- 开发人员非常有用的Java功能代码
- 非常有用的flex代码总结
- 15个非常有用的Android代码
- 开发人员非常有用的Java功能代码
- 非常有用的php代码片段
- 非常有用的java20个代码片段
- 分享非常有用的Java程序(关键代码)(八)---Java InputStream读取网络响应Response数据的方法!(重要)
- 读取XML文件非常便捷的方法
- 非常有用的Java日期时间操作函数代码一览
- Linux子系统系列-SPI
- Input 控件的Onchange 与onBlur 事件区别
- python文件支持中文注释
- 我的第一个 Symbian Qt 应用
- jquery基础(二)
- Silverlight读取XML代码(非常有用^-^)
- JRTPLib移植到OMAP3530平台
- 关于java 中的assert 关键字
- 程序员如何接项目
- 12种Javascript解决常见浏览器兼容问题的方法
- 隐身,是为了躲避失望
- 开始程序员生涯的五个建议
- 没有为 SQL 缓存通知启用数据库
- 积累编程经验的三种途径