RavenDB:XmlNodeConverter仅支持反序列化XmlDocuments

时间:2014-12-01 10:22:36

标签: xml json serialization ravendb

我无法从RavenDb反序列化“XmlElement”对象。我得到错误:“XmlNodeConverter仅支持反序列化XmlDocuments”。

类FooEventType的代码由Svcutil工具生成(来自wsdl和xsd文件)。

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:eglobal:eis:xsd:1")]
public partial class FooEventType 
{
    private Foo[] fooListField;

    private System.Xml.XmlElement[] anyField;

    [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    [System.Xml.Serialization.XmlArrayItemAttribute("foo", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public Foo[] fooList
    {
        get
        {
            return this.fooListField;
        }
        set
        {
            this.fooListField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElementAttribute(Order = 1)]
    public System.Xml.XmlElement[] Any
    {
        get
        {
            return this.anyField;
        }
        set
        {
            this.anyField = value;
        }
    }

}

在这个类中,有一个类型为“XmlElement”的“Any”属性,它在使用RavenDb进行Json反序列化时会导致出现问题。序列化期间没有问题。

在我的数据库中序列化的JSON对象:

{
  "fooList": [
    {
      "Value": "test1"
    },
    {
      "Value": "test2"
    }
  ],
  "Any": [
    {
      "testOfProp": {
        "#whitespace": [
          "\n",
          "\n"
        ],
        "testOfValue": "XYZ"
      }
    }
  ]
}

在数据库中查询期间反序列化时的异常详细信息:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=Could not convert document FooEventTypes/8961 to entity of type EWcfService.FooEventType
  Source=Raven.Client.Lightweight
  StackTrace:
       at Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity(Type entityType, String id, RavenJObject documentFound, RavenJObject metadata)
       at Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(Type entityType, String key, RavenJObject document, RavenJObject metadata, Boolean noTracking)
       at Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity[T](String key, RavenJObject document, RavenJObject metadata, Boolean noTracking)
       at Raven.Client.Document.SessionOperations.QueryOperation.Deserialize[T](RavenJObject result)
       at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Raven.Client.Document.SessionOperations.QueryOperation.Complete[T]()
       at Raven.Client.Document.AbstractDocumentQuery`2.GetEnumerator()
       at Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Epcis11WcfService.RavenDb.RavenDbEpcis.poll(pollRequest request) in d:\Activite\PROJ\DEV\Main\ESolution2\ESolution2\E11WcfService\RavenDb\RavenDbE.cs:line 99
       at E11WcfService.Query.poll(pollRequest request) in d:\Activite\PROJ\DEV\Main\ESolution2\ESolution2\E11WcfService\Query.svc.cs:line 47
       at SyncInvokepoll(Object , Object[] , Object[] )
       at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
  InnerException: Raven.Imports.Newtonsoft.Json.JsonSerializationException
       HResult=-2146233088
       Message=Could not read value for property: Any
       Source=Raven.Abstractions
       StackTrace:
            at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
            at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
            at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
            at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
            at Raven.Imports.Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
            at Raven.Imports.Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
            at Raven.Client.Connection.DocumentConventionJsonExtensions.Deserialize(RavenJObject self, Type type, DocumentConvention convention)
            at Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity(Type entityType, String id, RavenJObject documentFound, RavenJObject metadata)
       InnerException: Raven.Imports.Newtonsoft.Json.JsonSerializationException
            HResult=-2146233088
            Message=XmlNodeConverter only supports deserializing XmlDocuments
            Source=Raven.Abstractions
            StackTrace:
                 at Raven.Imports.Newtonsoft.Json.Converters.XmlNodeConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
                 at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
                 at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection wrappedList, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
                 at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
                 at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
                 at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
                 at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
            InnerException: 

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案。由于getter / setter的序列化,我将XmlElement []转换为XmlDocument [],并使用[XmlIgnore]和[JsonIgnore]属性。 但如果您有其他解决方案,我仍然感兴趣。

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:eglobal:eis:xsd:1")]
public partial class FooEventType 
{
    private Foo[] fooListField;

    private System.Xml.XmlDocument[] anyField;

    [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    [System.Xml.Serialization.XmlArrayItemAttribute("foo", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public Foo[] fooList
    {
        get
        {
            return this.fooListField;
        }
        set
        {
            this.fooListField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElementAttribute(Order = 1)]
    [JsonIgnore]
    public XmlElement[] Any
    {
        get
        {
            return this.anyField.Select(x => x.DocumentElement).ToArray();
        }
        set
        {
            if (value != null)
            {
                this.anyField = new XmlDocument[value.Count()];
                for (int i = 0; i < value.Count(); i++)
                {
                    this.anyField[i] = new XmlDocument();
                    this.anyField[i].LoadXml(value[i].OuterXml);
                }
            }


        }
    }

    [System.Xml.Serialization.XmlIgnore]
    [JsonProperty(PropertyName = "Any")]
    public System.Xml.XmlDocument[] Any_ForRavenJsonSerialization
    {
        get
        {
            return this.anyField;
        }

        set
        {
            this.anyField = value;
        }
    }

}