3

对此的任何帮助表示赞赏,我一直在努力。

从 HTTP 请求中,我收到一个 XML 响应。我无法控制它的结构,因此我必须将该结构与我的数据合同相匹配。但是,无论我做什么, Result 对象中的所有属性都是 null 或默认值。

XML结构:

<Customnamedroot xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Results="2" Skip="0">
<Object>
    <Something xsi:nil="true" />
    <Anything>2012-06-08T11:21:27</Anything>
    <Booleanthing>true</Booleanthing>
</Object>
<Object>
    <Something>1463387</Something>
    <Anything>2012-06-07T09:16:11.41</Anything>
    <Booleanthing>true</Booleanthing>
</Object>
</Customnamedroot>

来电:

SearchResults objects = response.Content.ReadAsAsync<SearchResults>().Result;

对应的数据契约类:

[CollectionDataContract(Name = "Customnamedroot", ItemName = "Object", Namespace = "")]
public class SearchResults : List<Result>
{
}

//[DataContract(Name = "Object")]
public class Result
{
    [DataMember(Order = 1, Name = "Something", IsRequired = false)]
    public decimal? Something{ get; set; }

    [DataMember(Order = 2, Name = "Anything", IsRequired = true, EmitDefaultValue = false)]
    public DateTime Anything{ get; set; }

    [DataMember(Order = 3, Name = "Booleanthing", IsRequired = true, EmitDefaultValue = false)]
    public bool Booleanthing{ get; set; }
}

编辑:如果省略 DataContract(Name = "Object"),则会出现此问题。如果添加了该 DataContractAttribute,我会收到以下错误:

System.Runtime.Serialization.SerializationException: Error in line 1 position 157. 
'EndElement' 'Object' from namespace '' is not expected. 
Expecting element 'Something| Anything'.

我究竟做错了什么?

4

1 回答 1

2

ItemNameCollectionDataContract中删除。为该类重新添加DataContract ,并使用空字符串向其中添加属性。如果在 xml 结构中没有明确定义命名空间,.Net 将假定一个空字符串作为命名空间,您必须将自己与此联系起来。ResultNamespace

[CollectionDataContract(Name = "Customnamedroot", Namespace = "")]
public class SearchResults : List<Result>
{
}

[DataContract(Name = "Object", Namespace = "")]
public class Result
{
    [DataMember(Order = 1, Name = "Something", IsRequired = false)]
    public decimal? Something{ get; set; }

    [DataMember(Order = 2, Name = "Anything", IsRequired = true, EmitDefaultValue = false)]
    public DateTime Anything{ get; set; }

    [DataMember(Order = 3, Name = "Booleanthing", IsRequired = true, EmitDefaultValue = false)]
    public bool Booleanthing{ get; set; }
}

作为旁注;即使您不控制 XML 的生成,您当然可以在将其反序列化为具体类之前使用 Linq to Xml 修改其结构。这只是我目前正在进行的转换的一个示例。通过转换 XML,您可以防止 XML 结构决定具体类的外观和组合方式,并且可以按照您喜欢的方式自由地执行 OOP。

protected string OnTransformXml(string xml)
{
    var doc = XDocument.Parse(xml);
    var root = doc.Descendants("root").FirstOrDefault();
    var allResults = doc.Descendants("Result").ToList();
    foreach (var node in allResults)
    {
        // remove all Result elements from their current location
        node.Remove();
        // rename the Result element to RawResult
        node.Name = "RawResult";
        // create a container for the Result elements to live in
        var rawMembersElement = new XElement("Result");
        // add each Result element to the new container
        rawMembersElement.Add(node);
        // add the new container
        root.Add(rawMembersElement);
    }
    return doc.ToString();
}

转换这个:

<root>
  <Result>
      <a>1</a>
      <b></b>
  </Result>
</root>

对此:

<root>
  <Result>
    <RawResult>
      <a>1</a>
      <b></b>
    </RawResult>
  </Result>
</root>
于 2014-10-24T02:43:30.577 回答