0

介绍

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


    }
4

1 回答 1

0

好吧,您做错了-您可能应该使用 XML 序列化-例如,请参阅此页面:http: //msdn.microsoft.com/en-us/library/182eeyhh.aspx

但是在您的代码中,错误是您在此处创建了 EMPTY 数组:

public amzReport[] Report;

你永远不会“扩展”它。我建议您将该行更改为

public List<amzReport> Report;

然后像这样更改您的主 foreach 循环:

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);
}
于 2013-09-03T06:03:38.040 回答