如何在运行时修改根 XML 标签?
How do I modify the root XML tag at runtime?
我是 XML 的新手,我通常在 JSON 处理我的 API,如果我想在这里重新发明轮子,请随时告诉我。
背景故事:我目前正在开发一种集成,它只允许我有一个端点来支持多个请求,即搜索、销售、更新、取消。他们只支持XML,所以我不能使用JSON。我根据根 XML 名称确定请求类型,完成我的工作,然后发回响应。
问题:由于这是 XML,我必须 return 一个强类型对象进行序列化,这使我无法使用许多自定义 classes 与 [XmlRoot(ElementName = "blah")]
。因此,我需要在 运行 时间设置根元素名称以支持我必须发送的不同命名回复。
我的回复class:
public class Response
{
public Errors Error { get; set; }
public RequestData Request { get; set; }
public List<Limo> Limos { get; set; }
public string ReservationID { get; set; }
public string Status { get; set; }
public string ConfNum { get; set; }
public string CancelPolicy { get; set; }
}
产生
的响应
<?xml version="1.0" encoding="utf-16"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Error>
<ErrorCode />
<ErrorSource />
<ErrorDescription />
</Error>
<Request>
<ServiceType>300</ServiceType>
<StartDateTime>2015-09-30T09:00:00</StartDateTime>
<NumPassengers>1</NumPassengers>
<VehicleType>100</VehicleType>
</Request>
<Limos />
</Response>
基本上,我需要能够根据需要将 <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
更改为 <SearchReply>
、<SellReply>
、<UpdateReply>
或 <CancelReply>
以结束像这样
<?xml version="1.0" encoding="utf-16"?>
<SearchReply>
<Error>
<ErrorCode />
<ErrorSource />
<ErrorDescription />
</Error>
<Request>
<ServiceType>300</ServiceType>
<StartDateTime>2015-09-30T09:00:00</StartDateTime>
<NumPassengers>1</NumPassengers>
<VehicleType>100</VehicleType>
</Request>
<Limos />
</SearchReply>
这是一个多部分修复。我对我如何让它发挥作用并不满意,但它确实有效 none-the-less。随着我在整个项目中学到更多,我可以重构。
根据 Pankaj 的建议:
我使用 Response
作为基础实现了派生的 class。这还不够,因为我的对象仍在获取基础 Response
class,但这是必要的,因为我需要为每次调用单独的 class 以完全实现下一步。
public class IntegrationSearchReply : Response
然后,我修饰派生的 classes 以指定根元素名称。
[XmlRoot(ElementName = "SearchReply", Namespace = "")]
public class IntegrationSearchReply : Response
这仍然没有用,所以我开始研究手动创建我的回复。我发现的最后一步是将我的控制器方法从返回 Response
更改为返回 HttpResponseMessage
。这使我能够使用 XmlSerializer
序列化对象并将 HttpResponseMessage
内容设置为序列化对象。
感谢您对 Pankaj 的帮助!
我是 XML 的新手,我通常在 JSON 处理我的 API,如果我想在这里重新发明轮子,请随时告诉我。
背景故事:我目前正在开发一种集成,它只允许我有一个端点来支持多个请求,即搜索、销售、更新、取消。他们只支持XML,所以我不能使用JSON。我根据根 XML 名称确定请求类型,完成我的工作,然后发回响应。
问题:由于这是 XML,我必须 return 一个强类型对象进行序列化,这使我无法使用许多自定义 classes 与 [XmlRoot(ElementName = "blah")]
。因此,我需要在 运行 时间设置根元素名称以支持我必须发送的不同命名回复。
我的回复class:
public class Response
{
public Errors Error { get; set; }
public RequestData Request { get; set; }
public List<Limo> Limos { get; set; }
public string ReservationID { get; set; }
public string Status { get; set; }
public string ConfNum { get; set; }
public string CancelPolicy { get; set; }
}
产生
的响应<?xml version="1.0" encoding="utf-16"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Error>
<ErrorCode />
<ErrorSource />
<ErrorDescription />
</Error>
<Request>
<ServiceType>300</ServiceType>
<StartDateTime>2015-09-30T09:00:00</StartDateTime>
<NumPassengers>1</NumPassengers>
<VehicleType>100</VehicleType>
</Request>
<Limos />
</Response>
基本上,我需要能够根据需要将 <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
更改为 <SearchReply>
、<SellReply>
、<UpdateReply>
或 <CancelReply>
以结束像这样
<?xml version="1.0" encoding="utf-16"?>
<SearchReply>
<Error>
<ErrorCode />
<ErrorSource />
<ErrorDescription />
</Error>
<Request>
<ServiceType>300</ServiceType>
<StartDateTime>2015-09-30T09:00:00</StartDateTime>
<NumPassengers>1</NumPassengers>
<VehicleType>100</VehicleType>
</Request>
<Limos />
</SearchReply>
这是一个多部分修复。我对我如何让它发挥作用并不满意,但它确实有效 none-the-less。随着我在整个项目中学到更多,我可以重构。
根据 Pankaj 的建议:
我使用 Response
作为基础实现了派生的 class。这还不够,因为我的对象仍在获取基础 Response
class,但这是必要的,因为我需要为每次调用单独的 class 以完全实现下一步。
public class IntegrationSearchReply : Response
然后,我修饰派生的 classes 以指定根元素名称。
[XmlRoot(ElementName = "SearchReply", Namespace = "")]
public class IntegrationSearchReply : Response
这仍然没有用,所以我开始研究手动创建我的回复。我发现的最后一步是将我的控制器方法从返回 Response
更改为返回 HttpResponseMessage
。这使我能够使用 XmlSerializer
序列化对象并将 HttpResponseMessage
内容设置为序列化对象。
感谢您对 Pankaj 的帮助!