Introduction
My goal is to convert a LIST into XML format. This list will consist of several "REPORT" nodes all with child elements.I am using XML Serialization to achieve this result.
The resulting XML, will appear as this:
<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>
ERROR:
ERROR:Object reference not set to an instance of an object.
reportXML.Report[index].ReportID = reportInfo.ReportId;
CODE(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);
}
My XML Class and Serialization Function:
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();
}
/********************************************************************************/
}