使用数组的XML序列化

本文关键字:XML 序列化 数组 | 更新日期: 2023-09-27 18:29:52

简介

我的目标是将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();
        }
        /********************************************************************************/

    }

使用数组的XML序列化

好吧,你做得不对-你可能应该使用XML序列化-例如,请参阅本页:http://msdn.microsoft.com/en-us/library/182eeyhh.aspx

但在你的代码中,错误是你在这里创建了EMPTY数组:

public amzReport[] Report;

而且你永远不会"扩展"它。我建议你把这条线改成

public List<amzReport> Report;

然后像这样改变你的前臂主循环:

foreach (ReportInfo reportInfo in reportInfoList)
{
    amzReport rpt = new amzReport();
    index++;
    HttpContext.Current.Response.Write("ReportInfo");
    if (reportInfo.IsSetReportId())
    {
         HttpContext.Current.Response.Write("ReportId");
         HttpContext.Current.Response.Write("" +  reportInfo.ReportId);
         rpt.ReportID = reportInfo.ReportId;
    }
    // all other code goes here...
    reportXML.Report.Add(rpt);
}