webService序列化xml 以及去掉刪除
/// 過車資訊查詢
/// </summary>
/// <param name="FetchId">索引ID值</param>
/// <param name="FetchCount">獲取的資料條數</param>
/// <returns></returns>
[WebMethod]
public void GetInCar(int FetchId, int FetchCount)
{
InCarResult.ROOT root = new InCarResult.ROOT();
List<InCarResult.ROOT> list = new List<InCarResult.ROOT>();
List<InCarResult.GCXX> list2 = new List<InCarResult.GCXX>();
try
{
List<VW_ParkOrderList> vWList = Parking.BLL.ParkOrder.GetVWList("TOP 100 *", "ParkOrderStatus_Name='已入場' order by ParkOrder_ID desc");
root.CODE = 0;
root.MESSAGE = "成功";
if (vWList.Count > 0)
{
for (int i = 0; i < vWList.Count; i++)
{
if (i >= FetchId && i < FetchId + FetchCount)
{
list2.Add(new InCarResult.GCXX
{
GCID = i,
GCCD = vWList[i].Parking_Key.ToString(),
GCSJ = vWList[i].ParkOrder_EnterTime.ToString(),
GCCP = vWList[i].ParkOrder_CarNo,
GCFX = "入場",
GCPT = "牌識放行",
GCTP = vWList[i].CarType_Name,
GCLN = vWList[i].ParkOrder_EnterGateName,
GCPI = vWList[i].Parking_ID.ToString(),
GCPP = vWList[i].ParkOrder_EnterImgPath == null ? null : SystemParameterConfig.Domain_Web + vWList[i].ParkOrder_EnterImgPath,
});
list.Add(root);
}
}
root.gcxxList.AddRange(list2);
}
string text = WebService.Serialize<InCarResult.ROOT>(root);
text = text.Replace("<gcxxList>", "");
text= text.Replace("</gcxxList>", "");
Context.Response.Write(text);
Context.Response.End();
}
catch (Exception ex)
{
LogHelper.Error(ex.Message.ToString());
root.CODE = 2;
root.MESSAGE = ex.Message.ToString();
}
Context.Response.Write(WebService.Serialize<InCarResult.ROOT>(root));
Context.Response.End();
}
/// <summary>
/// 序列化為XML
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string Serialize<T>(T t)
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add("", "");
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);
xmlWriterSettings.Indent = true;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
XmlWriter xmlWriter2;
XmlWriter xmlWriter = xmlWriter2 = XmlWriter.Create(memoryStream, xmlWriterSettings);
try
{
xmlSerializer.Serialize(xmlWriter, t, xmlSerializerNamespaces);
}
finally
{
if (xmlWriter2 != null)
{
((IDisposable)xmlWriter2).Dispose();
}
}
return xmlWriterSettings.Encoding.GetString(memoryStream.ToArray()).Trim();
}
1.把返回值型別改為void
2.Context.Response.Write()後面必須加上Context.Response.End()