DataSet和XML文件互操作的例子
//读取XML文件到DataSet
DataSet ds = new DataSet();
MyXmlPath = "C:\\Message.xml";
FileStream fs = new FileStream(MyXmlPath, FileMode.Open);
ds.ReadXml(fs, XmlReadMode.ReadSchema);
fs.Close();
//筛选行存入DataSet
DataRow[] FoundRows;
string Expression = "AREA IN ('" + Area + "',")";
string SortOrder = "DAY DESC";
FoundRows = ds.Tables[0].Select(Expression, SortOrder);
DataTable Mydt = new DataTable();
Mydt.Columns.Add("FKEY");
Mydt.Columns.Add("IIIII");
Mydt.Columns.Add("NAME");
Mydt.Columns.Add("AREA");
Mydt.Columns.Add("X");
Mydt.Columns.Add("Y");
Mydt.Columns.Add("BKIND");
Mydt.Columns.Add("SKIND");
Mydt.Columns.Add("DAY");
Mydt.Columns.Add("ETIME");
Mydt.Columns.Add("TYPE");
Mydt.Columns.Add("MAIN");
Mydt.Columns.Add("TTIME");
foreach (DataRow dr in FoundRows)
{
Mydt.Rows.Add(dr.ItemArray);
}
DataSet Myds = new DataSet();
Myds.Tables.Add(Mydt);
//保存到XML文件
Myds.WriteXml(MyXmlPath, XmlWriteMode.WriteSchema);
//ListView的一个例子
XmlDocument doc = new XmlDocument();
XmlDeclaration Declaration = doc.CreateXmlDeclaration("1.0","GB2312","");
doc.AppendChild(Declaration);
XmlElement xe = doc.CreateElement("Infos");
doc.AppendChild(xe);
for(int i = 0;i < listview.Items.Count;i++)
{
XmlNode parent = doc.CreateElement("Info" + i.ToString());
ListViewItem temp = listview.Items[i];
for(int j = 0;j < temp.SubItems.Count;j++)
{
string s = temp.SubItems[j].Text;
XmlNode node = doc.CreateElement(s);
node.InnerText = s;
parent.AppendChild(node);
}
doc.AppendChild(parent);
}
doc.Save(FilePath);