0

将以下代码传输到 XML 文件中的更简单方法是什么?我曾尝试使用 XDocument,但代码与所有 if 语句都非常混乱。

我的代码:

public class GetReportListSample
{


    public static void InvokeGetReportList(AmazonWebService service, GetReportListRequest request)
    {
        try
        {
            GetReportListResponse response = service.GetReportList(request);
            HttpContext.Current.Response.Write("=======STARTING REQUEST===================");

            if (response.IsSetGetReportListResult())
            {
                HttpContext.Current.Response.Write("            GetReportListResult");
                GetReportListResult getReportListResult = response.GetReportListResult;

                if (getReportListResult.IsSetNextToken())
                {
                     HttpContext.Current.Response.Write("                NextToken");
                     HttpContext.Current.Response.Write(""+ getReportListResult.NextToken);
                }

                if (getReportListResult.IsSetHasNext())
                {
                     HttpContext.Current.Response.Write("                HasNext");
                     HttpContext.Current.Response.Write("" + getReportListResult.HasNext);

                }


                List<ReportInfo> reportInfoList = getReportListResult.ReportInfo;
                foreach (ReportInfo reportInfo in reportInfoList)
                {
                     HttpContext.Current.Response.Write("                ReportInfo");
                     amzXML1.Add(new XElement("ReportInfo", ""));

                    if (reportInfo.IsSetReportId())
                    {
                         HttpContext.Current.Response.Write("                    ReportId");
                         HttpContext.Current.Response.Write("" +  reportInfo.ReportId);

                    }
                    if (reportInfo.IsSetReportType())
                    {
                         HttpContext.Current.Response.Write("                    ReportType");
                         HttpContext.Current.Response.Write("" +  reportInfo.ReportType);

                    }
                    if (reportInfo.IsSetReportRequestId())
                    {
                         HttpContext.Current.Response.Write("                    ReportRequestId");
                         HttpContext.Current.Response.Write("" +  reportInfo.ReportRequestId);

                    }
                    if (reportInfo.IsSetAvailableDate())
                    {
                         HttpContext.Current.Response.Write("                    AvailableDate");
                         HttpContext.Current.Response.Write("" +  reportInfo.AvailableDate);

                    }
                    if (reportInfo.IsSetAcknowledged())
                    {
                         HttpContext.Current.Response.Write("                    Acknowledged");
                         HttpContext.Current.Response.Write("" +  reportInfo.Acknowledged);

                    }
                    if (reportInfo.IsSetAcknowledgedDate())
                    {
                         HttpContext.Current.Response.Write("                    AcknowledgedDate");
                         HttpContext.Current.Response.Write("" +  reportInfo.AcknowledgedDate);

                    }
                }
            }
            if (response.IsSetResponseMetadata())
            {
                 HttpContext.Current.Response.Write("            ResponseMetadata");
                ResponseMetadata responseMetadata = response.ResponseMetadata;
                if (responseMetadata.IsSetRequestId())
                {
                     HttpContext.Current.Response.Write("                RequestId");
                     HttpContext.Current.Response.Write("" + responseMetadata.RequestId);
                }
            }

             HttpContext.Current.Response.Write("            ResponseHeaderMetadata");
             HttpContext.Current.Response.Write("                RequestId");
             HttpContext.Current.Response.Write("                    " + response.ResponseHeaderMetadata.RequestId);
             HttpContext.Current.Response.Write("                ResponseContext");
             HttpContext.Current.Response.Write("                    " + response.ResponseHeaderMetadata.ResponseContext);
             HttpContext.Current.Response.Write("                Timestamp");
             HttpContext.Current.Response.Write("                    " + response.ResponseHeaderMetadata.Timestamp);

        }
        catch (MarketplaceWebServiceException ex)
        {
             HttpContext.Current.Response.Write("Caught Exception: " + ex.Message);
             HttpContext.Current.Response.Write("Response Status Code: " + ex.StatusCode);
             HttpContext.Current.Response.Write("Error Code: " + ex.ErrorCode);
             HttpContext.Current.Response.Write("Error Type: " + ex.ErrorType);
             HttpContext.Current.Response.Write("Request ID: " + ex.RequestId);
             HttpContext.Current.Response.Write("XML: " + ex.XML);
             HttpContext.Current.Response.Write("ResponseHeaderMetadata: " + ex.ResponseHeaderMetadata);
        }
    }
}

这是 XML 应该看起来的样子(请记住,可能有几个 ReportInfo 元素:

<?xml version="1.0"?>
<GetReportListResponse xmlns="http://mws.amazonservices.com/doc/2009-01-01/">
  <GetReportListResult>
  <NextToken>2YgYW55IPQhvu5hbCBwbGVhc3VyZS4=</NextToken>
    <HasNext>true</HasNext>
    <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>
  </GetReportListResult>
  <ResponseMetadata>
    <RequestId>fbf677c1-dcee-4110-bc88-2ba3702e331b</RequestId>
  </ResponseMetadata>
</GetReportListResponse>  
4

1 回答 1

0

根据您的问题,我了解您需要 XML 文件作为可下载内容,这种理解是否正确?如何制作自己的普通实体或数据结构 [C#/VB 类与 [properties get/set]?

我的意思是,一旦你有了 .net 对象,你只需为其属性赋值并利用.Net 序列化[XML 或 JSON 任何你需要的],将序列化的内容放入 HTTPResponse 对象并指定响应内容类型。

从上面的示例结构 XML 中,元素将成为您在类中的字段或属性。提示:如果您有示例 XML 结构,则转换为 XSD 并使用XSD 命令行实用程序生成 .net 类 [VB 或 C#]。如果您手动编写,请不要忘记使用序列化属性 [Serializable] 标记您的类。

下面是序列化的代码示例,将此代码转换为您的辅助方法。

Object value = someobject; //Serializable object

XmlSerializer serializer = new XmlSerializer(value.GetType());
string xmlString;   // will contain XML string
StringWriter xmlStringWriter = null;  // String writer;
XmlWriter xmlWriter = null; // XML writer.

XmlWriterSettings writerSettings = new XmlWriterSettings  //XML writer settings
{
    OmitXmlDeclaration = true,
    NewLineOnAttributes = true
};
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
xmlNamespace.Add(string.Empty, string.Empty);// Remove namespaces of XML declaration.
try
{
    xmlStringWriter = new StringWriter(CultureInfo.CurrentCulture);
    xmlWriter = XmlWriter.Create(xmlStringWriter, writerSettings);
    serializer.Serialize(xmlWriter, value, xmlNamespace);
    xmlString = Convert.ToString(xmlStringWriter);
}
finally
{
    if (xmlStringWriter != null)
    {
        xmlStringWriter.Dispose();
    }
}

希望这将帮助您删除冗长的代码,而在序列化过程中对资源消耗成本的权衡可以忽略不计。.Net 确实使用序列化将内容从 Page 对象获取到响应对象的任何方式,所以如果事情做得正确,那么就没有问题。

于 2013-09-02T04:24:27.917 回答