可以使用WriteXml将SQL中的数据,写到Xml文件中。使用ds.WriteXml或者dt.WriteXml都可以。
下面是代码例子:
View Code
private void BindToGridView1(string StrPositionId)
{
//string sqlString = string.Empty;
List<SalesUserInfo> SalesList;
SalesList = new List<SalesUserInfo>();
string strHeaderPostnID = "1-258KID";
clsUser user = new clsUser();
List<UserInfo> TeamUser = new List<UserInfo>();
TeamUser = user.AuditTeamMember(strHeaderPostnID);
int count = 1;
for (int i = 0; i < TeamUser.Count; i++)
{
SalesUserInfo eSales = new SalesUserInfo();
eSales.LoginName = TeamUser[i].LoginName;
eSales.AliasName = TeamUser[i].AliasName;
eSales.PostnID = TeamUser[i].PostnID;
eSales.SortNo = string.Format("IAG{0:0#}", count);
eSales.Region = "华东PA";
eSales.Active = "Y";
eSales.ActiveForPAC = "Y";
eSales.ActiveForWOP = "Y";
count++;
SalesList.Add(eSales);
}
DataTable resultDataTable = new DataTable("IAG");
resultDataTable.Columns.Add("SortNo", Type.GetType("System.String"));
resultDataTable.Columns.Add("Region", Type.GetType("System.String"));
resultDataTable.Columns.Add("AliasName", Type.GetType("System.String"));
resultDataTable.Columns.Add("LoginName", Type.GetType("System.String"));
resultDataTable.Columns.Add("PostnID", Type.GetType("System.String"));
resultDataTable.Columns.Add("Active", Type.GetType("System.String"));
resultDataTable.Columns.Add("ActiveForPAC", Type.GetType("System.String"));
resultDataTable.Columns.Add("ActiveForWOP", Type.GetType("System.String"));
for (int i = 0; i < SalesList.Count; i++)
{
DataRow newRow = resultDataTable.NewRow();
newRow["SortNo"] = SalesList[i].SortNo;
newRow["Region"] = SalesList[i].Region;
newRow["AliasName"] = SalesList[i].AliasName;
newRow["LoginName"] = SalesList[i].LoginName;
newRow["PostnID"] = SalesList[i].PostnID;
newRow["Active"] = SalesList[i].Active;
newRow["ActiveForPAC"] = SalesList[i].ActiveForPAC;
newRow["ActiveForWOP"] = SalesList[i].ActiveForWOP;
resultDataTable.Rows.Add(newRow);
}
gvPSMListInfo.DataSource = resultDataTable;
gvPSMListInfo.DataBind();
string xmlFile = Server.MapPath("IAGSalesList.xml");
resultDataTable.WriteXml(xmlFile, XmlWriteMode.IgnoreSchema);
}
生成的IAGSalesList.xml文件如下:
xml文件
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<IAG>
<SortNo>IAG01</SortNo>
<Region>华东PA</Region>
<AliasName>PA_奇男代</AliasName>
<LoginName>WANGSHENG_IAG</LoginName>
<PostnID>1-DHYNQ</PostnID>
<Active>N</Active>
<ActiveForPAC>N</ActiveForPAC>
<ActiveForWOP>N</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG02</SortNo>
<Region>华东PA</Region>
<AliasName>PA_TBH</AliasName>
<LoginName>GUANGMINGMA</LoginName>
<PostnID>1-1R09EF</PostnID>
<Active>Y</Active>
<ActiveForPAC>N</ActiveForPAC>
<ActiveForWOP>N</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG03</SortNo>
<Region>华东PA</Region>
<AliasName>王举</AliasName>
<LoginName>WANGJU</LoginName>
<PostnID>1-HV10NE</PostnID>
<Active>Y</Active>
<ActiveForPAC>Y</ActiveForPAC>
<ActiveForWOP>Y</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG04</SortNo>
<Region>华东PA</Region>
<AliasName>张帷</AliasName>
<LoginName>WILSONZHANG</LoginName>
<PostnID>1-G9T9I4</PostnID>
<Active>N</Active>
<ActiveForPAC>N</ActiveForPAC>
<ActiveForWOP>N</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG05</SortNo>
<Region>华东PA</Region>
<AliasName>朱晓鹏</AliasName>
<LoginName>XIAOPENGZHU</LoginName>
<PostnID>1-LQKZM</PostnID>
<Active>Y</Active>
<ActiveForPAC>Y</ActiveForPAC>
<ActiveForWOP>Y</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG10</SortNo>
<Region>华东PA</Region>
<AliasName>苏俊杰</AliasName>
<LoginName>JUNJIESU</LoginName>
<PostnID>1-13ZN9Y</PostnID>
<Active>Y</Active>
<ActiveForPAC>Y</ActiveForPAC>
<ActiveForWOP>Y</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG11</SortNo>
<Region>MA</Region>
<AliasName>李国忠</AliasName>
<LoginName>VINCENTLIACN</LoginName>
<PostnID>1-1BXJ0J</PostnID>
<Active>N</Active>
<ActiveForPAC>N</ActiveForPAC>
<ActiveForWOP>N</ActiveForWOP>
</IAG>
<IAG>
<SortNo>IAG12</SortNo>
<Region>MA</Region>
<AliasName>宣大胜</AliasName>
<LoginName>DASHENGXUAN</LoginName>
<PostnID>1-TZHYX</PostnID>
<Active>Y</Active>
<ActiveForPAC>Y</ActiveForPAC>
<ActiveForWOP>Y</ActiveForWOP>
</IAG>
</DocumentElement>