DataSet ds=new DataSet("XMLProducts");
//连接SQL Server数据库
SqlConnection conn=new SqlConnection(@"server=glf;uid=sa;pwd=;database=northwind");
SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM products",conn);
//绑定DataGrid控件
da.Fill(ds,"products");
dataGrid1.DataSource=ds;
dataGrid1.DataMember="products";
doc=new XmlDataDocument(ds);
XmlNodeList nodeLst=doc.GetElementsByTagName("ProductName");
//将Xml结点加入item中
foreach(XmlNode nd in nodeLst)
listBox1.Items.Add(nd.InnerText);
//存盘
string file="product.xml";
ds.WriteXml(file);
//连接SQL Server数据库
SqlConnection conn=new SqlConnection(@"server=glf;uid=sa;pwd=;database=northwind");
SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM products",conn);
//绑定DataGrid控件
da.Fill(ds,"products");
dataGrid1.DataSource=ds;
dataGrid1.DataMember="products";
doc=new XmlDataDocument(ds);
XmlNodeList nodeLst=doc.GetElementsByTagName("ProductName");
//将Xml结点加入item中
foreach(XmlNode nd in nodeLst)
listBox1.Items.Add(nd.InnerText);
//存盘
string file="product.xml";
ds.WriteXml(file);
using System.Data.SqlClient;
using System.Data;
class TestMulti
{
public static void Main()
{
string sFileName = "MyTest.xml"; SqlDataAdapter da = new SqlDataAdapter("select * from authors; select * from titles; select * from sales",
"server=localhost;database=pubs;uid=sa;pwd=;"); da.TableMappings.Add("Table","authors");
da.TableMappings.Add("Table1","titles");
da.TableMappings.Add("Table2","sales"); DataSet ds = new DataSet();
da.Fill(ds); ds.WriteXml(sFileName, XmlWriteMode.WriteSchema); ds.Dispose();
ds = null;
ds = new DataSet();
ds.ReadXml(sFileName);
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine("{0}, rowcount:{1}", dt.TableName, dt.Rows.Count);
}
}
}
如果是有效的xml文件
using System;
using System.IO;
using System.Xml;//Reads an XML documentpublic class Sample
{
private const String filename = "items.xml"; public static void Main()
{
XmlTextReader reader = null; try
{
//Load the reader with the data file and ignore all white space nodes.
reader = new XmlTextReader(filename);
} finally
{
if (reader!=null)
reader.Close();
}
}}
我把文件改成合法了,也用XmlTextReader读出来 了,可是就是不知道怎么从reader对象里面读出一个值业
{
if(reader.NodeType==XmlNodeType.XmlDeclaration)
{
richTextBox1.AppendText("<?xml version='1.0' encoding='utf-8'?>"+"\n");
}
if(reader.NodeType==XmlNodeType.Element)
{
richTextBox1.AppendText("<"+reader.Name+">");
reader.Read();
}
if(reader.NodeType==XmlNodeType.Element)
{
richTextBox1.AppendText("\n"+"<"+reader.Name+">");
reader.Read();
}
if(reader.NodeType==XmlNodeType.Element)
richTextBox1.AppendText("\n"+"<"+reader.Name+">");
else if(reader.NodeType==XmlNodeType.Text)
richTextBox1.AppendText(reader.Value);
else if(reader.NodeType==XmlNodeType.EndElement)
richTextBox1.AppendText("</"+reader.Name+">"+"\n");}