介绍
我的目标是将 LIST 转换为 XML 格式。这个列表将包含几个带有子元素的“REPORT”节点。我正在使用 XML 序列化来实现这个结果。
生成的 XML 将如下所示:
<ReportInfo>
<ReportId>898899473</ReportId>
<ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType>
<ReportRequestId>2278662938</ReportRequestId>
<AvailableDate>2009-02-10T09:22:33+00:00</AvailableDate>
<Acknowledged>false</Acknowledged>
</ReportInfo>
<ReportInfo>
<ReportId>894899473</ReportId>
<ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType>
<ReportRequestId>227162938</ReportRequestId>
<AvailableDate>2009-02-10T09:22:33+00:00</AvailableDate>
<Acknowledged>false</Acknowledged>
</ReportInfo>
错误:
错误:对象引用未设置为对象的实例。
reportXML.Report[index].ReportID = reportInfo.ReportId;
代码(1):
/**********************************LOOP THROUGH LIST AND ADD TO XML****************************************************************************************/
List<ReportInfo> reportInfoList = getReportListResult.ReportInfo;
ReportListCatalog reportXML = new ReportListCatalog();
int index = -1;
foreach (ReportInfo reportInfo in reportInfoList)
{
index++;
HttpContext.Current.Response.Write("ReportInfo");
if (reportInfo.IsSetReportId())
{
HttpContext.Current.Response.Write("ReportId");
HttpContext.Current.Response.Write("" + reportInfo.ReportId);
reportXML.Report[index].ReportID = reportInfo.ReportId;
reportXML.Report[index].ReportID = reportInfo.ReportId;
}
if (reportInfo.IsSetReportType())
{
HttpContext.Current.Response.Write(" ReportType");
HttpContext.Current.Response.Write("" + reportInfo.ReportType);
reportXML.Report[index].ReportType = reportInfo.ReportType;
}
if (reportInfo.IsSetReportRequestId())
{
HttpContext.Current.Response.Write(" ReportRequestId");
HttpContext.Current.Response.Write("" + reportInfo.ReportRequestId);
reportXML.Report[index].ReportRequestId = reportInfo.ReportRequestId;
}
if (reportInfo.IsSetAvailableDate())
{
HttpContext.Current.Response.Write(" AvailableDate");
HttpContext.Current.Response.Write("" + reportInfo.AvailableDate);
reportXML.Report[index].ReportDate = reportInfo.AvailableDate;
}
if (reportInfo.IsSetAcknowledged())
{
HttpContext.Current.Response.Write(" Acknowledged");
HttpContext.Current.Response.Write("" + reportInfo.Acknowledged);
reportXML.Report[index].ReportAcknowledged = reportInfo.Acknowledged;
}
}
reportXML.SerializeToXML(reportXML);
}
我的 XML 类和序列化函数:
namespace AmazonWebService.Model
{
/***********************************************************************************/
[XmlRoot("ReportListCatalog")]
public class ReportListCatalog
{
[XmlArray("Report")]
public amzReport[] Report;
public class amzReport
{
[XmlArrayItem("ReportID")]
public String ReportID
{ get; set; }
[XmlArrayItem("ReportType")]
public String ReportType
{ get; set; }
[XmlArrayItem("ReportRequestId")]
public String ReportRequestId
{ get; set; }
[XmlArrayItem("AvailibleDate")]
public System.DateTime ReportDate
{ get; set; }
[XmlArrayItem("Acknowledged")]
public Boolean ReportAcknowledged
{ get; set; }
}
/********************************************************************************/
public void SerializeToXML(ReportListCatalog amzReport)
{
String SaveDir = "\\" + System.Web.HttpRuntime.AppDomainAppPath.ToString();
String dt = System.DateTime.Now.ToString("yyyy_dd_mm_hh_mm");
XmlSerializer serializer = new XmlSerializer(typeof(List<amzReport>));
TextWriter textWriter = new StreamWriter(SaveDir + dt + ".xml");
serializer.Serialize(textWriter, amzReport);
textWriter.Close();
}
/********************************************************************************/
}