BS架构下将数据导出为XML文件,及将XML文件数据导入数据库
导出
/// <summary>
/// 功能:生成XML文件
/// </summary>
private void CreateXML()
{
Manage.Components.DataAccess objData;
DataSet ds;
int n;
XmlDeclaration xmlDec;
XmlDocument xmlDoc;
XmlElement xmlEltDocument;
XmlElement xmlEltName;
XmlElement xmlEltQuestions;
XmlElement xmlEltAnswers;
XmlElement xmlEltReceiptQuestions;
XmlElement xmlEltReceiptAnswers;
XmlElement xmlEltColumm;
try
{
//初始化XmlDocument
xmlDoc = new XmlDocument();
///添加说明
xmlDec = xmlDoc.CreateXmlDeclaration("1.0","UTF-8","");
xmlDoc.AppendChild(xmlDec);
xmlDec = null;
//添加Document节点
xmlEltDocument = xmlDoc.CreateElement("Document");
xmlDoc.AppendChild(xmlEltDocument);
//创建Name
xmlEltName = xmlDoc.CreateElement("Name");
xmlEltName.InnerText = "名称" + DateTime.Now.ToString();
xmlEltDocument.AppendChild(xmlEltName);
//判断当前用户是否是客户
bool blnIsCustomer = Common.Common.GetIsCustomer();
string strUserFlag;
if(blnIsCustomer)
{
strUserFlag = "Customer";
}
else
{
strUserFlag = "Commpany";
}
//创建用户标志
xmlEltName = xmlDoc.CreateElement("UserFlag");
xmlEltName.InnerText = strUserFlag;
xmlEltDocument.AppendChild(xmlEltName);
objData = new Support.Manage.Components.DataAccess();
//创建问题表
if(blnIsCustomer)
{
ds = objData.CustomerQuestionsGet(); //客户
}
else
{
ds = objData.CompanyQuestionsGet(); //公司
}
n = ds.Tables[0].Rows.Count;
if(ds != null && n > 0)
{
//创建Questions
for(int i = 0; i < n; i++)
{
DataRow dr = ds.Tables[0].Rows[i];
//创建Questions
xmlEltQuestions = xmlDoc.CreateElement("Questions");
xmlEltDocument.AppendChild(xmlEltQuestions);
//创建Questions列 f_QuestionID
xmlEltColumm = xmlDoc.CreateElement("f_QuestionID");
xmlEltColumm.InnerText = dr["f_QuestionID"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_CustomerCode
xmlEltColumm = xmlDoc.CreateElement("f_CustomerCode");
xmlEltColumm.InnerText = dr["f_CustomerCode"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_AppCode
xmlEltColumm = xmlDoc.CreateElement("f_AppCode");
xmlEltColumm.InnerText = dr["f_AppCode"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_Content
xmlEltColumm = xmlDoc.CreateElement("f_Content");
xmlEltColumm.InnerText = dr["f_Content"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_Questioner
xmlEltColumm = xmlDoc.CreateElement("f_Questioner");
xmlEltColumm.InnerText = dr["f_Questioner"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_Phone
xmlEltColumm = xmlDoc.CreateElement("f_Phone");
xmlEltColumm.InnerText = dr["f_Phone"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_QuestionDate
xmlEltColumm = xmlDoc.CreateElement("f_QuestionDate");
xmlEltColumm.InnerText = dr["f_QuestionDate"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_IsSended
xmlEltColumm = xmlDoc.CreateElement("f_IsSended");
xmlEltColumm.InnerText = dr["f_IsSended"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_IPAddress
xmlEltColumm = xmlDoc.CreateElement("f_IPAddress");
xmlEltColumm.InnerText = dr["f_IPAddress"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
//创建Questions列 f_RowFlag
xmlEltColumm = xmlDoc.CreateElement("f_RowFlag");
xmlEltColumm.InnerText = dr["f_RowFlag"].ToString();
xmlEltQuestions.AppendChild(xmlEltColumm);
}
}
////保存到本地
//xmlDoc.Save("d://Support.xml");
//用IE打开
Response.Clear();
Response.Write(xmlDoc.OuterXml);
Response.Flush();
Response.End();
}
catch(Exception e)
{
Common.Client.ShowErrorDialog(this,e.Message);
}
finally
{
objData = null;
ds = null;
xmlDec = null;
xmlDoc = null;
xmlEltDocument = null;
xmlEltName = null;
xmlEltQuestions = null;
xmlEltAnswers = null;
xmlEltReceiptQuestions = null;
xmlEltReceiptAnswers = null;
xmlEltColumm = null;
}
}
导入
/// <summary>
/// 功能:导入XML数据
/// </summary>
private void ImportXML()
{
Manage.Components.DataAccess objData;
DataSet ds;
int n;
XmlDocument xmlDoc;
XmlNodeList xmlNList;
string strXmlFilePath;
try
{
objData = new Support.Manage.Components.DataAccess();
xmlDoc = new XmlDocument();
string strFileType = strFile.PostedFile.ContentType;
//如果选择的文件不是XML格式,无法导入
if(strFileType != "application/x-xml" && strFileType != "text/xml")
{
Common.Client.ShowErrorDialog(this,"您选择的文件不是XML格式,无法导入!");
return;
}
//上传Xml附件
strXmlFilePath = Server.MapPath("../XmlData") + "//SupportData.xml";
if (File.Exists(strXmlFilePath))
{
File.Delete(strXmlFilePath);
}
strFile.PostedFile.SaveAs(strXmlFilePath);
//加载Xml文件
xmlDoc.Load(strXmlFilePath);
//判断当前用户是否是客户
bool blnIsCustomer = Common.Common.GetIsCustomer();
string strUserFlag;
if(blnIsCustomer)
{
strUserFlag = "Customer";
}
else
{
strUserFlag = "Commpany";
}
//用户标志
xmlNList = xmlDoc.GetElementsByTagName("UserFlag");
string strUser = xmlNList.Item(0).InnerText.ToString();
//如果是自己导出的文件,则无法导入
if(strUser == strUserFlag)
{
Common.Client.ShowErrorDialog(this,"您选择的是自己导出的文件,无法导入!");
if (File.Exists(strXmlFilePath))
{
File.Delete(strXmlFilePath);
}
return;
}
//问题
xmlNList = null;
xmlNList = xmlDoc.GetElementsByTagName("Questions");
//已接受问题状态改为对方已确认状态
ds = objData.ReceiptQuestionsGet();
n = ds.Tables[0].Rows.Count;
for(int i = 0; i < n; i++)
{
string strQuestionID = ds.Tables[0].Rows[i]["f_QuestionID"].ToString();
string strCustomerCode = ds.Tables[0].Rows[i]["f_CustomerCode"].ToString();
int m = 0;
//遍历要导入的问题数据
foreach(XmlNode xmlItem in xmlNList)
{
string strQuestionIDGeted = xmlItem["f_QuestionID"].InnerText.ToString();
string strCustomerCodeGeted = xmlItem["f_CustomerCode"].InnerText.ToString();
if(strQuestionID == strQuestionIDGeted && strCustomerCode == strCustomerCodeGeted)
{
break;
}
m++;
}
//若要导入的数据中不含有此问题,则将此问题状态改为对方已确认状态
if(xmlNList.Count == m)
{
objData.QuestionID = Convert.ToInt32(strQuestionID);
objData.CustomerCode = strCustomerCode;
objData.QuestionSendedSet();
}
}
//循环将问题导入
foreach(XmlNode xmlItem in xmlNList)
{
objData.QuestionID = Convert.ToInt32(xmlItem["f_QuestionID"].InnerText.ToString());
objData.CustomerCode = xmlItem["f_CustomerCode"].InnerText.ToString();
objData.AppCode = xmlItem["f_AppCode"].InnerText.ToString();
objData.QuestionContent = xmlItem["f_Content"].InnerText.ToString();
objData.Questioner = xmlItem["f_Questioner"].InnerText.ToString();
objData.Phone = xmlItem["f_Phone"].InnerText.ToString();
objData.QuestionDate = Convert.ToDateTime(xmlItem["f_QuestionDate"].InnerText.ToString());
//objData.IsSended = Convert.ToInt32(xmlItem["f_IsSended"].InnerText.ToString());
objData.IPAddress = xmlItem["f_IPAddress"].InnerText.ToString();
objData.RowFlag = Convert.ToInt32(xmlItem["f_RowFlag"].InnerText.ToString());
objData.QuestionAdd();
}
//导入成功后,删除已上传的Xml文件
if (File.Exists(strXmlFilePath))
{
File.Delete(strXmlFilePath);
}
Common.Client.ShowErrorDialog(this,"导入成功!");
}
catch(Exception e)
{
Common.Client.ShowErrorDialog(this,e.Message);
}
finally
{
objData = null;
xmlDoc = null;
xmlNList = null;
}
}