将下面的代码转移到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>
答案 0 :(得分:0)
基于您的问题我理解的是您需要XML文件作为可下载内容,这种理解是否正确? 如何制作自己的普通实体或数据结构[C#/ VB类与[属性获取/设置]?
我的意思是,一旦你拥有.net对象,你只需为其属性赋值并利用.Net Serialization [XML或JSON,无论你需要什么],将序列化内容放在HTTPResponse对象中并指定响应内容类型。
从上面的示例结构XML中,元素将成为您的类中的字段或属性。 提示:如果您有XML结构示例,则转换为XSD并使用XSD command line实用程序生成.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对象传递到响应对象,这样如果事情正确完成则没有问题。