///读写操作 private void DemonstrateReadWriteXMLDocumentWithXMLReader(){ // Create a DataSet with one table and two columns. DataSet OriginalDataSet = new DataSet("myDataSet"); OriginalDataSet.Namespace= "NetFrameWork"; DataTable myTable = new DataTable("myTable"); DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32")); c1.AutoIncrement= true; DataColumn c2 = new DataColumn("item"); myTable.Columns.Add(c1); myTable.Columns.Add(c2); OriginalDataSet.Tables.Add(myTable); // Add ten rows. DataRow newRow; for(int i = 0; i < 10; i++){ newRow = myTable.NewRow(); newRow["item"]= "item " + i; myTable.Rows.Add(newRow); } OriginalDataSet.AcceptChanges(); // Print out values of each table in the DataSet using the // function defined below. PrintValues(OriginalDataSet, "Original DataSet"); // Write the XML schema and data to file with FileStream. string xmlFilename = "myXmlDocument.xml"; // Create FileStream System.IO.FileStream fsWriteXml = new System.IO.FileStream (xmlFilename, System.IO.FileMode.Create); // Create an XmlTextWriter to write the file. System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter (fsWriteXml, System.Text.Encoding.Unicode); // Use WriteXml to write the document. OriginalDataSet.WriteXml(xmlWriter); // Close the FileStream. fsWriteXml.Close();
// Dispose of the original DataSet. OriginalDataSet.Dispose(); // Create a new DataSet. DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document back in. // Create new FileStream to read schema with. System.IO.FileStream fsReadXml = new System.IO.FileStream (xmlFilename, System.IO.FileMode.Open); // Create an XmlTextReader to read the file. System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml); // Read the XML document into the DataSet. newDataSet.ReadXml(myXmlReader); // Close the XmlTextReader myXmlReader.Close(); // Print out values of each table in the DataSet using the // function defined below. PrintValues(newDataSet,"New DataSet"); }private void PrintValues(DataSet ds, string label){ Console.WriteLine("\n" + label); foreach(DataTable t in ds.Tables){ Console.WriteLine("TableName: " + t.TableName); foreach(DataRow r in t.Rows){ foreach(DataColumn c in t.Columns){ Console.Write("\t " + r[c] ); } Console.WriteLine(); } } } ===================================
private void DemonstrateReadWriteXMLDocumentWithXMLReader(){
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable myTable = new DataTable("myTable");
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement= true;
DataColumn c2 = new DataColumn("item");
myTable.Columns.Add(c1);
myTable.Columns.Add(c2);
OriginalDataSet.Tables.Add(myTable);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++){
newRow = myTable.NewRow();
newRow["item"]= "item " + i;
myTable.Rows.Add(newRow);
}
OriginalDataSet.AcceptChanges();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(OriginalDataSet, "Original DataSet");
// Write the XML schema and data to file with FileStream.
string xmlFilename = "myXmlDocument.xml";
// Create FileStream
System.IO.FileStream fsWriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Create an XmlTextWriter to write the file.
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(fsWriteXml, System.Text.Encoding.Unicode);
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter);
// Close the FileStream.
fsWriteXml.Close();
// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader.Close(); // Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,"New DataSet");
}private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
===================================
/// 读取xml
/// </summary>
/// <param name="filename">文件路径及名称</param>
/// <param name="xpath">路径</param>
/// <param name="attrname">属性名称</param>
/// <returns>返回值(字符串)</returns>
public string ReadXmlFile(string filename,string xpath,string attrname)
{
string strRtn = "";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);
XmlElement xRoot = xDoc.DocumentElement;
XmlNode xNode = xRoot.SelectSingleNode(xpath);
if(xNode.Attributes.Count>0 && xNode.Attributes[attrname]!=null)
{
strRtn = xNode.Attributes[attrname].Value.ToString();
}
xDoc = null;
return strRtn;
}
DataSet a
a.ReadXml(xml文件名);
docSQL.Load("SQL.xml");
XmlNode sql = docSQL.SelectSingleNode("/SAMS/SQL");
if (sql != null)
{
XmlNodeReader nr = new XmlNodeReader(sql);
strSQL=nr.ReadElementString();
}
docSQL.Load("SQL.xml");
XmlNode sql = docSQL.SelectSingleNode("/SAMS/SQL");
if (sql != null)
{
XmlNodeReader nr = new XmlNodeReader(sql);
strSQL=nr.ReadElementString();
}
/// 读取xml
/// </summary>
/// <param name="filename">文件路径及名称</param>
/// <param name="xpath">路径</param>
/// <param name="attrname">属性名称</param>
/// <returns>返回值(字符串)</returns>
public string ReadXmlFile(string filename,string xpath,string attrname)
{
string strRtn = "";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);
XmlElement xRoot = xDoc.DocumentElement;
XmlNode xNode = xRoot.SelectSingleNode(xpath);
if(xNode.Attributes.Count>0)
{
XmlAttribute xAtt=xNode.Attributes;
strRtn = xAtt.[attrname].Value;
}
xDoc = null;
return strRtn;
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<remoteserver>
<connstring Server="SQLName" />
</remoteserver>
</root>
读取:
string strServer = ReadXmlFile(Application.StartupPath+@"\Conn.xml",@"remoteserver/connstring","Server");
我都可以读取,如果不能,提示什么问题?
{
reader = new XmlTextReader(path);
reader.WhitespaceHandling = WhitespaceHandling.None; //不处理空白节点
myHash = new Hashtable();
doc = new XmlDocument();
doc.Load(reader);
root = doc.DocumentElement; //根节点
nodeList = root.ChildNodes; foreach(XmlNode node in nodeList)
{
//node.Name为节点的名称即AppPath, node.InnerText为节点的值,
//node.Attributes["AppName"].InnerText 为属性AppName的值 myHash.Add(node.Attributes["PathNameHashKey"].InnerText ,node.InnerText);
myHash.Add(node.Attributes["AppNameHashKey"].InnerText ,node.Attributes["AppName"].InnerText);
myHash.Add(node.Attributes["AppName"].InnerText,node.InnerText);
}
//myHash.Add("Count" ,nodeList.Count); return myHash;
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ReadingXML
{
/// <summary>
/// 读取XML文件并将其内容显示在DataGrid组件中。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnReadXML;
private System.Windows.Forms.Button btnShowSchema;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.panel1 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnShowSchema = new System.Windows.Forms.Button();
this.btnReadXML = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(570, 23);
this.label1.TabIndex = 0;
this.label1.Text = "读取XML文件中的数据";
this.label1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 23);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(570, 403);
this.dataGrid1.TabIndex = 1;
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.btnShowSchema,
this.btnReadXML});
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 258);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(570, 168);
this.panel1.TabIndex = 5;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(570, 120);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "textBox1";
//
// btnShowSchema
//
this.btnShowSchema.Location = new System.Drawing.Point(160, 128);
this.btnShowSchema.Name = "btnShowSchema";
this.btnShowSchema.Size = new System.Drawing.Size(75, 32);
this.btnShowSchema.TabIndex = 5;
this.btnShowSchema.Text = "显示架构";
this.btnShowSchema.Click += new System.EventHandler(this.btnShowSchema_Click);
//
// btnReadXML
//
this.btnReadXML.Location = new System.Drawing.Point(24, 128);
this.btnReadXML.Name = "btnReadXML";
this.btnReadXML.Size = new System.Drawing.Size(88, 32);
this.btnReadXML.TabIndex = 4;
this.btnReadXML.Text = "读取XML";
this.btnReadXML.Click += new System.EventHandler(this.btnReadXML_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(570, 426);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1,
this.dataGrid1,
this.label1});
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "读取XML数据";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} // 申明一个数据集
DataSet dsAddress = new DataSet("address");
// 读取XML文件,并将读到的数据内容显示在DataGrid组件中。
private void btnReadXML_Click(object sender, System.EventArgs e)
{
string filePath = "address.xml";
dsAddress.ReadXml(filePath);
dataGrid1.DataSource = dsAddress;
dataGrid1.DataMember = "address";
dataGrid1.CaptionText = dataGrid1.DataMember;
}
// 显示XML文件的框件结构
private void btnShowSchema_Click(object sender, System.EventArgs e)
{
System.IO.StringWriter swXML = new System.IO.StringWriter();
dsAddress.WriteXmlSchema(swXML);
textBox1.Text = swXML.ToString();
}
}
}
这个应该可以帮到你!