您的位置:首页 > 编程语言 > C#

c# 大量拼接xml时内存溢出解决方法

2014-01-07 11:16 393 查看
public static string SelectUNnormalPriceSTrans(EUNnormalPriceS rqInfo)
{
string guidStrJianJclFirst = Guid.NewGuid().ToString().Replace("-", "").ToUpper();
string resultFileName = @"E:\PPrice\jianjcl" + guidStrJianJclFirst + ".xml";
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(resultFileName)))
{ System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(resultFileName)); }

XmlTextWriter result = new XmlTextWriter(resultFileName, null);
try
{
if (rqInfo.Success == true)
{
result.WriteStartElement("BasicUnNormalPrice");
result.WriteStartElement("UnNormalPrices");
NPPrice port = new NPPrice();
if (rqInfo.listunpbi != null && rqInfo.listunpbi.Count > 0)
{
for (int i = 0; i < rqInfo.listunpbi.Count; i++)
{
port = (NPPrice)rqInfo.listunpbi[i];
result.WriteStartElement("UnNormalPrice");
// 特价运价ID|航司|出发|到达|销售开始日期|销售结束日期|旅行开始日期|旅行结束日期|舱位等级|舱位|票面价|折扣|运价基础|...|运价来源|修改时间
string listValue = port.ID + "|" + port.AirCompany + "|" + port.DepartCode + "|" + port.ArriveCode.ToString() + "|" + port.SaleBeginTime.ToString() + "|" + port.SaleEndTime.ToString() + "|" + port.TravelBeginTime.ToString() + "|" + port.TravelEndTime.ToString() + "|" + port.Price.ToString() + "|" + 0 + "|" + port.PriceBase.ToString() + "|" + port.Agio + "|" + port.PriceSource + "|" + port.AdvanceStartDay.ToString() + "|" + port.AdvanceEndDay.ToString() + "||||" + port.ApplyFlightNumber.ToString() + "|" + port.WeekNum + "|" + port.AddTime.ToString() + "|" + port.Cabin.ToString() + "|" + port.NotApplyFlightNumber.ToString() + "|||" + port.ApplyIntervalTime.ToString() + "|" + port.ModifyTime.ToString() + "|" + port.Status.ToString();

result.WriteString(listValue);
result.WriteEndElement();
listValue = null;
}
}
result.WriteEndElement();
result.WriteEndElement();
result.Flush();
result.Close();
GC.Collect();
XmlDocument xml = new XmlDocument();
xml.Load(resultFileName);
return xml.OuterXml;
}
else
{
return "<BasicUnNormalPrice><UnNormalPrices><BasicUnNormalPrice><Error Code='900001'>" + rqInfo.Msg.ToString() + "</Error></BasicUnNormalPrice></UnNormalPrices></BasicUnNormalPrice>";
}
}
catch (Exception ex) {  return "<BasicUnNormalPrice><UnNormalPrices><BasicUnNormalPrice><Error Code='900001'>" + ex.Message.ToString() + "</Error></BasicUnNormalPrice></UnNormalPrices></BasicUnNormalPrice>"; }
finally
{
File.Delete(resultFileName);
GC.Collect();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: