将对象序列化为XML而不使用空元素和具有空子元素的元素

时间:2015-12-11 10:22:45

标签: c# .net xml serialization xsd2code

我需要将对象序列化为XML请求,但是我调用的服务不接受空元素。

我从XSD生成合同类,请求结构如下所示:

/* Generated contracts - simplified & without XML attributes */
public class Request
{
    public Request()
    {
        filters = new RequestFilters();
        properties = new ResultProperties();
    }

    public RequestFilters filters { get; set; }

    public ResultProperties properties { get; set; }
}

public class ResultProperties
{
    public ResultProperties()
    {
        props = new List<string>();
    }
    public List<string> props { get; set; }
}

public class RequestFilters
{
    public RequestFilters()
    {
        ids = new LongFilter();
        names = new StringFilter();
    }

    public LongFilter ids { get; set; }
    public StringFilter names { get; set; }
}

public class LongFilter
{
    public LongFilter()
    {
        values = new List<long>();
    }

    public List<long> values { get; set; }
}

public class StringFilter
{
    public StringFilter()
    {
        values = new List<string>();
    }

    public List<string> values { get; set; }
}

对于代码生成,我使用 xsd2code 并将其配置为从序列化中排除空集合。创建对象时

Request request = new Request();

ResultProperties rp = new ResultProperties();
rp.props.Add("foo");
rp.props.Add("bar");

request.properties = rp;

我可以将其序列化为XML:

<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <filters>
    <ids />
    <names />
  </filters>
  <properties>
    <props>
      <string>foo</string>
      <string>bar</string>
    </props>
  </properties>
</Request>

然而,后端服务会抱怨空<ids /><names />元素。

是否有首选/任何方法从序列化XML中排除这些空元素?

如果我设法排除<ids /><names />,最终我也希望摆脱<filters>元素。感谢。

更新:添加了XSD的相关部分

<xs:schema>
  <xs:element name="Request">
    <xs:complexType>
      <xs:sequence>             
        <xs:element name="filters" type="ouext:RequestFilters" minOccurs="0" />
        <xs:element name="properties" type="ouext:ResultProperties" minOccurs="0" />
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="RequestFilters">
      <xs:sequence>
        <xs:element name="ids" type="filters:LongFilter" minOccurs="0" />
        <xs:element name="names" type="filters:StringFilter" minOccurs="0" />
      <xs:sequence>
    </xs:complexType>

    <xs:complexType name="LongFilter">
      <xs:sequence>
        <xs:element name="values" type="filters:LongInFilter" minOccurs="0" />
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="LongInFilter">
      <xs:sequence>
        <xs:element name="value" type="xs:long" minOccurs="0" maxOccurs="20000" />
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="StringFilter">
      <xs:sequence>
        <xs:element name="values" type="filters:StringInFilter" minOccurs="0" />
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="StringInFilter">
      <xs:sequence>
        <xs:element name="value" type="xs:string" minOccurs="0" maxOccurs="20000" />
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="ResultProperties">
      <xs:sequence>
        <xs:element name="props" type="filters:StringInFilter" minOccurs="0" maxOccurs="unbounded">
        </xs:element>
      </xs:sequence>
    </xs:complexType>

  </xs:element>
</xs:schema>

更新:序列化按如下方式完成

string xmlPayload = null;
XmlSerializer serializer = new XmlSerializer(typeof(Request));
using (StringWriter sw = new StringWriter())
{
    using (XmlWriter xw = XmlWriter.Create(sw))
    {
        serializer.Serialize(xw, payload); // payload is initialized object of Request type
        xmlPayload = sw.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

我通过创建InitMethods来解决这个问题,这会使未使用的字段和集合无效。

所以不要做

Request request = new Request();

ResultProperties rp = new ResultProperties();
rp.props.Add("foo");
rp.props.Add("bar");

request.properties = rp;

我在做

Request InitRequest(RequestFilters filters = null, ResultProperties properties = null)
{
     Request request = new Request();
     request.filters = filters;
     request.properties = properties;

     return request;
}

RequestFilters InitRequestFilters(LongFilter ids = null, StringFilter names = null)
{
     RequestFilters filters = new RequestFilters();
     filters.ids = ids;
     filters.names = names;
}

初始化ResultPropertiesLongFilterStringFilter对象也是如此。

然后我使用InitMethods创建请求:

List<string> properties = new List<string> { "foo", "bar" };
ResultProperties rp = InitResultProperties(properties);

Request request = InitRequest(properties: rp);

当我序列化request时,我只得到包含实际值的XML元素,并且序列化中省略了空元素:

<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <properties>
    <props>
      <string>foo</string>
      <string>bar</string>
    </props>
  </properties>
</Request>

这不是最佳解决方案,因为创建InitMethods的开销很大,但我没有做出更好的解决方案。