cru*_*ush 6 c# wcf xml-serialization datacontractserializer .net-4.5
让我先说我对WCF很新,可能在这里使用了错误的术语.我的项目有两个组成部分:
我有一个XML模式,如下所示:
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="Attachment">
<xsd:complexType>
<xsd:all>
<xsd:element name="Extension" type="Extension" minOccurs="0" />
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:complexType>
<xsd:sequence name="Extension">
<xsd:any processContents="skip" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
遵循此模式,我有一个以下类型的XML文档:
<Attachment>
<Extension>
<ReportType1>
<Name>Report Type 1 Example</Name>
</ReportType1>
</Extension>
</Attachment>
Run Code Online (Sandbox Code Playgroud)
我在已编译的DLL中有以下类:
public class Attachment
{
public Extension Extension { get; set; }
}
public class Extension
{
[XmlElement(ElementName = "ReportType1", IsNullable = false)]
public ReportType1 ReportType1 { get; set; }
[XmlElement(ElementName = "ReportType2", IsNullable = false)]
public ReportType2 ReportType2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的WCF服务将XML文档反序列化为上述对象,然后通过以下OperationContract以JSON格式返回它:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
Attachment Search();
Run Code Online (Sandbox Code Playgroud)
实际输出为JSON
{
'Attachment': {
'Extension': {
'ReportType1': { ... },
'ReportType2': null
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际输出为XML
<Attachment>
<Extension>
<ReportType1>...</ReportType1>
<ReportType2 i:nil="true"></ReportType2>
</Extension>
</Attachment>
Run Code Online (Sandbox Code Playgroud)
期望的输出为JSON
{
'Attachment': {
'Extension': {
'ReportType1': { ... }
}
}
}
Run Code Online (Sandbox Code Playgroud)
期望输出为XML
<Attachment>
<Extension>
<ReportType1>...</ReportType1>
</Extension>
</Attachment>
Run Code Online (Sandbox Code Playgroud)
DLL中的类没有DataContract属性,但是当OperationContract我从上面发回来时,似乎序列化很好,因为我得到了上面的结果.
如果它们为null而没有将类从DLL转换为DataContract类的能力,我怎么能告诉它不要将元素序列化为JSON/XML ?我应该从DLL继承类,并以某种方式覆盖它们DataContract?如果是这样,我怎么能在基类的现有成员上设置属性呢?
如果需要更多信息,请告诉我,我会尽力提供.
| 归档时间: |
|
| 查看次数: |
1574 次 |
| 最近记录: |