1、此例为获取 Xml 文件内的数据页面内容:后台代码using System;using System.Windows.Controls;using System.Xml;using System.IO;using System.Text;using System.Xml.Linq;namespace ToTestDatapublic partial class MainPage : UserControlpublic MainPage()InitializeComponent();StringBuilder output = new StringBuilder();string author
2、= “;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
3、(“);break;elseif (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();
4、/分段显示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(“Th
5、e 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;以下为效果截屏: