DataSet对XML进行生成、更新、添加、删除等操作
//创建一个数据集
DataSet dsTradeNews = new DataSet();
//读取XML文件用于绑定到DataGrid上
dsTradeNews.ReadXml(@"D:\CorpInfo.xml",XmlReadMode.Auto);
//创建一个表
DataTable dtTradeNews = new DataTable("TradeNews");
//创建列
DataColumn dcSimpleName = new DataColumn("SimpleName",Type.GetType("System.String"));
DataColumn dcCode = new DataColumn("Code",Type.GetType("System.String"));
//将列加入到表中
dtTradeNews.Columns.Add(dcSimpleName);
dtTradeNews.Columns.Add(dcCode);
//创建行并为每列赋值
DataRow drTradeNew = dtTradeNews.NewRow();
drTradeNew["SimpleName"] = "公司";
drTradeNew["Code"] = "888888";
//将表加入数据集中
dsTradeNews.Tables.Add(dtTradeNews);
//生成XML文件或覆盖XML文件
try
{
dsTradeNews.WriteXml(@"D:\CorpInfo.xml");
}
catch(Exception ex)
{
Response.Write("生成XML文件产生异常:" + ex.Message);
return ;
}
dgTradeNews.DataSource = dsTradeNews.Tables["TradeNews"].DefaultView;
dgTradeNews.DataBind();
//创建一个数据集
DataSet dsTradeNews = new DataSet();
//读取XML文件用于绑定到DataGrid上
dsTradeNews.ReadXml(@"D:\CorpInfo.xml",XmlReadMode.Auto);
//创建一个表
DataTable dtTradeNews = new DataTable("TradeNews");
//创建列
DataColumn dcSimpleName = new DataColumn("SimpleName",Type.GetType("System.String"));
DataColumn dcCode = new DataColumn("Code",Type.GetType("System.String"));
//将列加入到表中
dtTradeNews.Columns.Add(dcSimpleName);
dtTradeNews.Columns.Add(dcCode);
//创建行并为每列赋值
DataRow drTradeNew = dtTradeNews.NewRow();
drTradeNew["SimpleName"] = "公司";
drTradeNew["Code"] = "888888";
//将表加入数据集中
dsTradeNews.Tables.Add(dtTradeNews);
//生成XML文件或覆盖XML文件
try
{
dsTradeNews.WriteXml(@"D:\CorpInfo.xml");
}
catch(Exception ex)
{
Response.Write("生成XML文件产生异常:" + ex.Message);
return ;
}
dgTradeNews.DataSource = dsTradeNews.Tables["TradeNews"].DefaultView;
dgTradeNews.DataBind();
另一种操作方式:
利用:doc、XmlNode
//创建XmlDocument的实例
private XmlDocument doc = new XmlDocument();
private XmlElement X_TR;
private XmlElement X_Script;
private XmlElement X_Connection;
private XmlElement X_Time;
private XmlElement X_Name;
private XmlElement X_Path;
private XmlText X_Script_Text;
private XmlText X_Connection_Text;
private XmlText X_Time_Text;
private XmlText X_Name_Text;
private XmlText X_Path_Text;
const string cfileName = "XMLnote.xml";
const string XmlFile = "ScriptMain";
const string XmlTab = "TAB";
const string XmlConnection = "Connection";
const string XmlScript = "Script";
const string XmlPath = "Path";
const string XmlName = "Name";
const string XmlTime = "Time";
//XML文档起点
private string XMLOP = "";
doc.Load(cfileName);
XmlNode Root = doc.DocumentElement;
string XMLstart = GetXMLOP();
XmlNode XNode = doc.SelectSingleNode(XMLstart);
if(XNode != null)
{
//产生行元素
X_TR = doc.CreateElement("",XmlTab,"");
//产生Script元素
X_Connection = doc.CreateElement("",XmlConnection,"");
X_Script = doc.CreateElement("",XmlScript,"");
X_Path = doc.CreateElement("",XmlPath,"");
X_Name = doc.CreateElement("",XmlName,"");
X_Time = doc.CreateElement("",XmlTime,"");
//产生该元素的文本
X_Connection_Text = doc.CreateTextNode(XMLLogList.Connection);
X_Script_Text = doc.CreateTextNode(XMLLogList.Script);
X_Path_Text = doc.CreateTextNode(XMLLogList.Path);
X_Name_Text = doc.CreateTextNode(XMLLogList.Name);
X_Time_Text = doc.CreateTextNode(XMLLogList.Time.ToString());
//向Script元素添加文本
X_Connection.AppendChild(X_Connection_Text);
X_Script.AppendChild(X_Script_Text);
X_Path.AppendChild(X_Path_Text);
X_Name.AppendChild(X_Name_Text);
X_Time.AppendChild(X_Time_Text);
//将Script元素添加到行节点下
X_TR.AppendChild(X_Connection);
X_TR.AppendChild(X_Script);
X_TR.AppendChild(X_Path);
X_TR.AppendChild(X_Name);
X_TR.AppendChild(X_Time);
//将行元素添加到父节点下
XNode.AppendChild(X_TR);
}
doc.Save(cfileName);
//创建XmlDocument的实例
private XmlDocument doc = new XmlDocument();
private XmlElement X_TR;
private XmlElement X_Script;
private XmlElement X_Connection;
private XmlElement X_Time;
private XmlElement X_Name;
private XmlElement X_Path;
private XmlText X_Script_Text;
private XmlText X_Connection_Text;
private XmlText X_Time_Text;
private XmlText X_Name_Text;
private XmlText X_Path_Text;
const string cfileName = "XMLnote.xml";
const string XmlFile = "ScriptMain";
const string XmlTab = "TAB";
const string XmlConnection = "Connection";
const string XmlScript = "Script";
const string XmlPath = "Path";
const string XmlName = "Name";
const string XmlTime = "Time";
//XML文档起点
private string XMLOP = "";
doc.Load(cfileName);
XmlNode Root = doc.DocumentElement;
string XMLstart = GetXMLOP();
XmlNode XNode = doc.SelectSingleNode(XMLstart);
if(XNode != null)
{
//产生行元素
X_TR = doc.CreateElement("",XmlTab,"");
//产生Script元素
X_Connection = doc.CreateElement("",XmlConnection,"");
X_Script = doc.CreateElement("",XmlScript,"");
X_Path = doc.CreateElement("",XmlPath,"");
X_Name = doc.CreateElement("",XmlName,"");
X_Time = doc.CreateElement("",XmlTime,"");
//产生该元素的文本
X_Connection_Text = doc.CreateTextNode(XMLLogList.Connection);
X_Script_Text = doc.CreateTextNode(XMLLogList.Script);
X_Path_Text = doc.CreateTextNode(XMLLogList.Path);
X_Name_Text = doc.CreateTextNode(XMLLogList.Name);
X_Time_Text = doc.CreateTextNode(XMLLogList.Time.ToString());
//向Script元素添加文本
X_Connection.AppendChild(X_Connection_Text);
X_Script.AppendChild(X_Script_Text);
X_Path.AppendChild(X_Path_Text);
X_Name.AppendChild(X_Name_Text);
X_Time.AppendChild(X_Time_Text);
//将Script元素添加到行节点下
X_TR.AppendChild(X_Connection);
X_TR.AppendChild(X_Script);
X_TR.AppendChild(X_Path);
X_TR.AppendChild(X_Name);
X_TR.AppendChild(X_Time);
//将行元素添加到父节点下
XNode.AppendChild(X_TR);
}
doc.Save(cfileName);
另一种操作XML的方式:
利用FileStream、XmlTextWriter操作XML文件
//第一次生成XML记录文件
FileStream XMLStream = new FileStream(cfileName,FileMode.CreateNew);
XmlTextWriter XmlWrite = new XmlTextWriter(XMLStream,Encoding.Unicode);
XmlWrite.Formatting = Formatting.Indented;
try
{
XMLBuilWriter(XmlWrite,XMLLogList);
XmlWrite.Close();
}
catch(Exception ex)
{
cRemark = ex.Message.ToString();
}
//开始创建XML文件
static void XMLBuilWriter(XmlWriter Write,LogListAttribute XMLLogList)
{
Write.WriteStartElement(XmlFile);
Write.WriteStartElement(XmlTab);
Write.WriteElementString(XmlConnection,XMLLogList.Connection);
Write.WriteElementString(XmlScript,XMLLogList.Script);
Write.WriteElementString(XmlPath,XMLLogList.Path);
Write.WriteElementString(XmlName,XMLLogList.Name);
Write.WriteElementString(XmlTime,DateTime.Now.ToLongTimeString());
Write.WriteEndElement();
Write.WriteEndElement();
}
//第一次生成XML记录文件
FileStream XMLStream = new FileStream(cfileName,FileMode.CreateNew);
XmlTextWriter XmlWrite = new XmlTextWriter(XMLStream,Encoding.Unicode);
XmlWrite.Formatting = Formatting.Indented;
try
{
XMLBuilWriter(XmlWrite,XMLLogList);
XmlWrite.Close();
}
catch(Exception ex)
{
cRemark = ex.Message.ToString();
}
//开始创建XML文件
static void XMLBuilWriter(XmlWriter Write,LogListAttribute XMLLogList)
{
Write.WriteStartElement(XmlFile);
Write.WriteStartElement(XmlTab);
Write.WriteElementString(XmlConnection,XMLLogList.Connection);
Write.WriteElementString(XmlScript,XMLLogList.Script);
Write.WriteElementString(XmlPath,XMLLogList.Path);
Write.WriteElementString(XmlName,XMLLogList.Name);
Write.WriteElementString(XmlTime,DateTime.Now.ToLongTimeString());
Write.WriteEndElement();
Write.WriteEndElement();
}