【问题标题】:RavenDB : XmlNodeConverter only supports deserializing XmlDocumentsRavenDB:XmlNodeConverter 仅支持反序列化 XmlDocuments
【发布时间】:2014-12-01 10:22:35
【问题描述】:

我无法反序列化来自 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: 

【问题讨论】:

    标签: xml json serialization ravendb


    【解决方案1】:

    我找到了这个解决方案。我在序列化时将 XmlElement[] 转换为 XmlDocument[],这要归功于 getter/setter,并且我使用 [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;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 2019-10-07
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      相关资源
      最近更新 更多