C# 带数组的XML序列化

C# 带数组的XML序列化,c#,asp.net,xml,xml-serialization,amazon-mws,C#,Asp.net,Xml,Xml Serialization,Amazon Mws,导言 我的目标是将列表转换为XML格式。这个列表将由几个“REPORT”节点组成,所有节点都有子元素 生成的XML将如下所示: <ReportInfo> <ReportId>898899473</ReportId> <ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType> <ReportRequestId>2278662938</Re

导言

我的目标是将列表转换为XML格式。这个列表将由几个“REPORT”节点组成,所有节点都有子元素

生成的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>
/**********************************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);


            }
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();
        }
        /********************************************************************************/


    }

898899473
_获取\商户\物品\数据_
2278662938
2009-02-10T09:22:33+00:00
假的
894899473
_获取\商户\物品\数据_
227162938
2009-02-10T09:22:33+00:00
假的
错误:

<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>
/**********************************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);


            }
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();
        }
        /********************************************************************************/


    }
错误:对象引用未设置为对象的实例

reportXML.Report[index].ReportID=reportInfo.ReportID

代码(1):

<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>
/**********************************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);


            }
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****************************************************************************************/
List reportInfoList=getReportListResult.ReportInfo;
ReportListCatalog reportXML=新的ReportListCatalog();
int指数=-1;
foreach(ReportInfo列表中的ReportInfo ReportInfo)
{
索引++;
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;
}
如果(reportInfo.IsSetAvailableDate())
{
HttpContext.Current.Response.Write(“AvailableDate”);
HttpContext.Current.Response.Write(“+reportInfo.AvailableDate”);
reportXML.Report[index].ReportDate=reportInfo.AvailableDate;
}
如果(reportInfo.IsSetAcknowledged())
{
HttpContext.Current.Response.Write(“已确认”);
HttpContext.Current.Response.Write(“+reportInfo.conferenced”);
reportXML.Report[index].ReportAcknowledged=reportInfo.Acknowledged;
}
}
reportXML.SerializeToXML(reportXML);
}
我的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>
/**********************************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);


            }
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();
        }
        /********************************************************************************/


    }
名称空间AmazonWebService.Model
{
/***********************************************************************************/
[XmlRoot(“ReportListCatalog”)]
公共类ReportListCatalog
{
[XmlArray(“报告”)]
公共amzReport[]报告;
公共类amzReport
{
[XmlArrayItem(“报告ID”)]
公共字符串报告ID
{get;set;}
[XmlArrayItem(“报告类型”)]
公共字符串报告类型
{get;set;}
[XmlArrayItem(“ReportRequestId”)]
公共字符串ReportRequestId
{get;set;}
[XmlArrayItem(“可用日期”)]
public System.DateTime报告日期
{get;set;}
[XmlArrayItem(“确认”)]
公共布尔报告已确认
{get;set;}
}
/********************************************************************************/
public void serializedXML(ReportListCatalog amzReport)
{
字符串SaveDir=“\\”+System.Web.HttpRuntime.AppDomainAppPath.ToString();
字符串dt=System.DateTime.Now.ToString(“yyyy\u dd\u mm\u hh\u mm”);
XmlSerializer serializer=新的XmlSerializer(typeof(List));
TextWriter TextWriter=newstreamWriter(SaveDir+dt+“.xml”);
serializer.Serialize(textWriter,amzReport);
textWriter.Close();
}
/********************************************************************************/
}

好吧,您这样做是错误的-您可能应该使用XML序列化-例如,请参阅本页:

但在代码中,错误在于您在此处创建空数组:

public amzReport[] Report;
你永远不会“扩展”它。我建议你把这行改成

public List<amzReport> Report;