收藏 分销(赏)

SilverLight获取XML数据(二).doc

上传人:xrp****65 文档编号:7709718 上传时间:2025-01-13 格式:DOC 页数:3 大小:105.08KB
下载 相关 举报
SilverLight获取XML数据(二).doc_第1页
第1页 / 共3页
SilverLight获取XML数据(二).doc_第2页
第2页 / 共3页
点击查看更多>>
资源描述
此例为获取Xml文件内的数据 页面内容: <UserControl x:Class="ToTestData.MainPage" xmlns=" xmlns:x=" xmlns:d=" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"> <Grid x:Name="LayoutRoot" Background="White"> <Button x:Name="btnTestone" Width="60" Height="40" Background="Pink"></Button> <TextBlock x:Name ="OutputTextBlock" TextWrapping="Wrap" /> <TextBox x:Name="txtTestone" Canvas.Top="20" Canvas.Left="10" TextWrapping="Wrap" Margin="90,215,277,63" Width="49" Height="22" IsEnabled="False" FontStyle="Italic" Foreground="Blue"></TextBox> </Grid> </UserControl> 后台代码 using System; using System.Windows.Controls; using System.Xml; using System.IO; using System.Text; using System.Xml.Linq; namespace ToTestData { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); StringBuilder output = new StringBuilder(); string author = ""; int count = 0; // XmlXapResolver is the default resolver. using (XmlReader reader = XmlReader.Create("Data/book.xml"))//括号内为路径 { // Moves the reader to the root element. reader.MoveToContent(); for (int i = 0; ; i++) { reader.ReadToFollowing("book"); author=reader.ReadOuterXml(); if (author == "") { output.AppendLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); break; } else { if (i != 0) output.AppendLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); int a = i + 1; output.AppendLine("Read the "+a+" book using ReadOuterXml..."); output.AppendLine(author); if (i == 0) { output.AppendLine("-----------------------------------------"); ShowDetail(output, author); } count += 1; } } output.AppendLine("一共 " + count + "个节点"); } OutputTextBlock.Text = output.ToString(); } //分段显示 private void ShowDetail(StringBuilder output, string author) { using (XmlReader reader = XmlReader.Create(new StringReader(author))) { reader.ReadToFollowing("book"); reader.MoveToFirstAttribute(); string genre = reader.Value; reader.MoveToNextAttribute(); string genre2 = reader.Value; output.AppendLine("The genre value: " + genre); output.AppendLine("The ISBN value: " + genre2); output.AppendLine("Content of the title element: " + GteXml(reader, "title", author)); txtTestone.Text = GteXml(reader, "price", author); btnTestone.Content = genre; } } //依据标题获取XML中内容 private static string GteXml(XmlReader reader, string name, string author) { reader.ReadToFollowing(name); author = reader.ReadInnerXml(); return author; } } } 以下为效果截屏:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服