将以下代码传输到 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>