1. if your xml is two levels deep and very regular, use DataSet
2. if your xml is not very large, use DOM classes in System.Xml
3. if your xml is large and you only need to update some known nodes, use XmlTextReader to read through and update the specified nodes and write to a stream directly
XmlTextReader类源于XmlReader类。XmlTextReader类可用来读取XML文档。此类的Read方法读取此文档,直到其节点结束。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Xml;
public class WebForm2 : Page
{
public StringBuilder outputQ;
public StringBuilder outputXml;
public DocumentNavigator nav = null;
public HtmlInputFile XmlFile;
public System.Web.UI.WebControls.Xml MyXml;
public System.Web.UI.WebControls.TextBox TextBox1;
public System.Web.UI.WebControls.TextBox TextBox2;
public System.Web.UI.WebControls.TextBox TextBox3;
public System.Web.UI.WebControls.Button Query;
public System.Web.UI.WebControls.Label FileLabel;
public void On_KeyUp(object sender, System.EventArgs e)
{
Response.Write("Works");
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//
// Evals true first time browser hits the page
//
}
}
public void Query_Click(object sender, System.EventArgs e)
{
HttpPostedFile xmlfile = XmlFile.PostedFile;
XmlDocument doc = new XmlDocument();
MyXml.Document = new XmlDocument();
// TextBox2.Text="";
// TextBox3.Text="";
if (xmlfile.FileName != String.Empty)
{
try
{
FileLabel.Text= xmlfile.FileName;
MyXml.Document.Load(xmlfile.FileName);
outputXml = new StringBuilder();
XmlTextReader reader = new XmlTextReader (xmlfile.FileName);
ShowDocument();
TextBox3.Text = outputXml.ToString();
outputQ = new StringBuilder();
doc.Load(xmlfile.FileName);
DocumentNavigator nav = new DocumentNavigator(doc);
// Perform the query e.g. "descendant::book/price"
XPathQuery(nav, TextBox1.Text);
TextBox2.Text = outputQ.ToString();
outputQ = new StringBuilder();
doc.Load(FileLabel.Text);
DocumentNavigator nav = new DocumentNavigator(doc);
// Perform the query e.g. "descendant::book/price"
XPathQuery(nav, TextBox1.Text);
TextBox2.Text = outputQ.ToString();
//***************************** Navigator ************************************
private void FormatXml (XmlNavigator navigator)
{
while (navigator.MoveToNextSelected())
{
switch (navigator.NodeType)
{
case XmlNodeType.ProcessingInstruction:
Format (navigator, "ProcessingInstruction");
break;
case XmlNodeType.DocumentType:
Format (navigator, "DocumentType");
break;
case XmlNodeType.Document:
Format (navigator, "Document");
break;
case XmlNodeType.Comment:
Format (navigator, "Comment");
break;
case XmlNodeType.Element:
Format (navigator, "Element");
break;
case XmlNodeType.Text:
Format (navigator, "Text");
break;
case XmlNodeType.Whitespace:
Format (navigator, "Whitespace");
break;
}
}
outputQ.Append("\r\n");
}
// Format the output
private void Format (XmlNavigator navigator, String NodeType)
{
String value = String.Empty;
String name = String.Empty;
if (navigator.HasChildren)
{
name = navigator.Name;
navigator.MoveToFirstChild();
if (navigator.HasValue)
{
value = navigator.Value;
}
}
else
{
if (navigator.HasValue)
{
value = navigator.Value;
name = navigator.Name;
}
}
outputQ.Append(NodeType + "<" + name + ">" + value);
outputQ.Append("\r\n");
}
// ********************************** XmlReader *****************************
public void ShowDocument ()
{
outputXml = new StringBuilder();
XmlTextReader reader = new XmlTextReader (FileLabel.Text);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction");
break;
case XmlNodeType.DocumentType:
Format (reader, "DocumentType");
break;
case XmlNodeType.Comment:
Format (reader, "Comment");
break;
case XmlNodeType.Element:
Format (reader, "Element");
break;
case XmlNodeType.Text:
Format (reader, "Text");
break;
case XmlNodeType.Whitespace:
break;
}
}
TextBox3.Text = outputXml.ToString();
}
protected void Format(XmlReader reader, String NodeType)
{
// Format the output
for (int i=0; i < reader.Depth; i++)
{
outputXml.Append('\t');
}