📄 form1.cs
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace WSonSPDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void menuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
XmlTextReader reader = null;
StreamWriter writer = null;
private void menuItem3_Click(object sender, EventArgs e)
{
writer = new StreamWriter(@"\Storage Card\output.txt", false);
try
{
int timeSpan1;
// int timeSpan2;
int t1 = System.Environment.TickCount;
reader = new XmlTextReader(@"\Storage Card\books.xml");
while (reader.Read())
{
//XmlNodeType这个枚举让我们方便的对不同类型的节点进行相应的操作
switch (reader.NodeType)
{
case XmlNodeType.XmlDeclaration:
FormatOutput(writer, reader, "XmlDeclaration");
break;
case XmlNodeType.ProcessingInstruction:
FormatOutput(writer, reader, "ProcessingInstruction");
break;
case XmlNodeType.DocumentType:
FormatOutput(writer, reader, "DocumentType");
break;
case XmlNodeType.Comment:
FormatOutput(writer, reader, "Comment");
break;
case XmlNodeType.Element:
FormatOutput(writer, reader, "Element");
break;
case XmlNodeType.Text:
FormatOutput(writer, reader, "Text");
break;
case XmlNodeType.Whitespace:
break;
}
}
int t2 = System.Environment.TickCount;
timeSpan1 = t2-t1;
MessageBox.Show("Complete,timeSpan="+timeSpan1+" ms");
}
catch (XmlException xmlEx)
{
writer.WriteLine(xmlEx.Message);
}
finally
{
reader.Close();
writer.Close();
}
}
private static void FormatOutput(StreamWriter writer, XmlReader reader, String nodeType)
{
//用tab使输出的层次分明一点
for (int i = 0; i < reader.Depth; i++)
{
writer.Write('\t');
}
//这里并没有做过多的判断,但是作为Element的节点并没有Value
if (reader.Name != String.Empty)
writer.WriteLine(reader.Prefix + nodeType + "<" + reader.Name + ">:" + reader.Value);
else
writer.WriteLine(reader.Prefix + nodeType + ": " + reader.Value);
// 显示当前节点的属性名和值,同样用tab做了格式控制
while (reader.MoveToNextAttribute())
{
for (int i = 0; i < reader.Depth; i++)
writer.Write('\t');
writer.WriteLine("Attribute: " + reader.Name + "= " + reader.Value);
}
}
private void menuItem4_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
/*
/// <summary>
/// 根据特定的XPath表达式查询节点信息
/// </summary>
/// <param name="xpath"></param>
private void SpecificQuery(string xpath)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"\Storage Card\books.xml");
XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("/BookStore/Book[author/last-name='Sheldon']");
foreach (XmlNode book in nodeList)
{
Console.WriteLine(book.Name+":"+book.Value);
}
}
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -