SqlConnecting conn = new SqlConnection(CONNSTR); SqlCommand cmd = new SqlCommand("select fname from employee for xml auto",conn); conn.open(); XmlReader reader = cmd.ExecuteXmlReader(); ...... ################ 所取xml数据格式 #################
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml;
/**//// /// Xml文件的读写类 /// /// public class XmlRW ...{ public XmlRW() ...{ // // TODO: 在此处添加构造函数逻辑 // }
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml;
/**//// /// Xml文件的读写类 /// /// public class XmlRW ...{ public XmlRW() ...{ // // TODO: 在此处添加构造函数逻辑 // }
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class XmlTest1 : System.Web.UI.Page ...{ protected void Page_Load(object sender, EventArgs e) ...{
遍历元素属性 string fileName = "..\\..\\..\\books.xml"; XmlTextReader tr = new XmlTextReader(fileName); while(tr.Read()) { //check to see if it"s a NodeType element if(tr.NodeType == XmlNodeType.Element) { //if it"s an element, then let"s look at the attributes. for(int i = 0; i < tr.AttributeCount; i++) { listBox1.Items.Add(tr.GetAttribute(i)); } }
------------------------------------------------------------------------ 写例程 ------------------------------------------------------------------------ XmlTextWriter writer = new XmlTextWriter (filename, null);
//Use indenting for readability. writer.Formatting = Formatting.Indented;
//xml声明(Write the XML delcaration. ) writer.WriteStartDocument();
//预处理指示(Write the ProcessingInstruction node.) String PItext="type="text/xsl" href="book.xsl""; writer.WriteProcessingInstruction("xml-stylesheet", PItext);
//文档类型(Write the DocumentType node.) writer.WriteDocType("book", null , null, "");
//注释(Write a Comment node.) writer.WriteComment("sample XML");
//根元素(Write a root element.) writer.WriteStartElement("book");
//属性值(Write the genre attribute.) writer.WriteAttributeString("genre", "novel");
//属性值(Write the ISBN attribute.) writer.WriteAttributeString("ISBN", "1-8630-014");
//Write the title. writer.WriteElementString("title", "The Handmaid"s Tale");
//Write the style element. writer.WriteStartElement("style"); writer.WriteEntityRef("h"); writer.WriteEndElement();
//文本元素节点(Write the price.) writer.WriteElementString("price", "19.95");
//[CDATA] writer.WriteCData("Prices 15% off!!");
//Write the close tag for the root element. writer.WriteEndElement();
writer.WriteEndDocument();
//Write the XML to file and close the writer. writer.Flush(); writer.Close();
//Load the file into an XmlDocument to ensure well formed XML. XmlDocument doc = new XmlDocument(); //Preserve white space for readability. doc.PreserveWhitespace = true; //Load the file. doc.Load(filename);
//Display the XML content to the console. Console.Write(doc.InnerXml);
The Autobiography of Benjamin Franklin Benjamin Franklin
8.99
...
books.xdr
cs using System.Xml.Schema; protected void button1_Click (object sender, System.EventArgs e) { //change this to match your path structure. string fileName = "..\\..\\..\\booksVal.xml"; XmlTextReader tr = new XmlTextReader(fileName); XmlValidatingReader trv = new XmlValidatingReader(tr);
//Set validation type trv.ValidationType=ValidationType.XDR; //Add in the Validation eventhandler trv.ValidationEventHandler += new ValidationEventHandler(this.ValidationEvent);
//Read in node at a time while(trv.Read()) { if(trv.NodeType == XmlNodeType.Text) listBox1.Items.Add(trv.Value); } } public void ValidationEvent (object sender, ValidationEventArgs args) { MessageBox.Show(args.Message); }
xml文件如下:
123 456 789 123 467
利用C#读写代码如下
using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Windows.Forms;