反序列化xml <result xmlns =“”>不是预期的</result>

时间:2014-08-04 08:52:33

标签: c# xml serialization

我有点麻烦找出导致此问题的原因以及解决方法。

谷歌为其他有同样错误的人后,它仍然有效。所以我希望有人可以帮助或引导我朝着正确的方向前进。

我试图将xml从Web服务反序列化为对象。

xml:

<Result>
    <UserStatus Status="34" StatusFrom="140729143619" ClientType="4" 
                LogOnServer="NcoreConnect" SIPServer="10.16.3.10" Device="tel:201" 
                DeviceStatus="0" DeviceStatusFrom="140729134828" ServerTime="140731090408" 
                IsLoggedOn="True" IsAgent="True" CallCount="0" AnsweredCount="0" 
                Interactions="" Message="" SessionID="a123a38d-d00d-47c0-af03-2b63f40fb8a6" 
                DiversionStatus="0" DiversionDestination="" OldMessageCount="0" 
                NewMessageCount="0">BR</UserStatus>
</Result>

然后我将xml粘贴为类

UserStatus.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace project.ExternalWebServices
{
  /// <remarks/>
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  //[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  [XmlRoot("Result")]
  public partial class Result
  {
    private ResultUserStatus userStatusField;

    /// <remarks/>
    public ResultUserStatus UserStatus
    {
        get
        {
            return this.userStatusField;
        }
        set
        {
            this.userStatusField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResultUserStatus
{
    private byte statusField;
    private ulong statusFromField;
    private byte clientTypeField;
    private string logOnServerField;
    private string sIPServerField;
    private string deviceField;
    private byte deviceStatusField;
    private ulong deviceStatusFromField;
    private ulong serverTimeField;
    private string isLoggedOnField;
    private string isAgentField;
    private byte callCountField;
    private byte answeredCountField;
    private string interactionsField;
    private string messageField;
    private string sessionIDField;
    private byte diversionStatusField;
    private string diversionDestinationField;
    private byte oldMessageCountField;
    private byte newMessageCountField;
    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Status
    {
        get
        {
            return this.statusField;
        }
        set
        {
            this.statusField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ulong StatusFrom
    {
        get
        {
            return this.statusFromField;
        }
        set
        {
            this.statusFromField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte ClientType
    {
        get
        {
            return this.clientTypeField;
        }
        set
        {
            this.clientTypeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string LogOnServer
    {
        get
        {
            return this.logOnServerField;
        }
        set
        {
            this.logOnServerField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string SIPServer
    {
        get
        {
            return this.sIPServerField;
        }
        set
        {
            this.sIPServerField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Device
    {
        get
        {
            return this.deviceField;
        }
        set
        {
            this.deviceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte DeviceStatus
    {
        get
        {
            return this.deviceStatusField;
        }
        set
        {
            this.deviceStatusField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ulong DeviceStatusFrom
    {
        get
        {
            return this.deviceStatusFromField;
        }
        set
        {
            this.deviceStatusFromField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ulong ServerTime
    {
        get
        {
            return this.serverTimeField;
        }
        set
        {
            this.serverTimeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsLoggedOn
    {
        get
        {
            return this.isLoggedOnField;
        }
        set
        {
            this.isLoggedOnField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsAgent
    {
        get
        {
            return this.isAgentField;
        }
        set
        {
            this.isAgentField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte CallCount
    {
        get
        {
            return this.callCountField;
        }
        set
        {
            this.callCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte AnsweredCount
    {
        get
        {
            return this.answeredCountField;
        }
        set
        {
            this.answeredCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Interactions
    {
        get
        {
            return this.interactionsField;
        }
        set
        {
            this.interactionsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Message
    {
        get
        {
            return this.messageField;
        }
        set
        {
            this.messageField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string SessionID
    {
        get
        {
            return this.sessionIDField;
        }
        set
        {
            this.sessionIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte DiversionStatus
    {
        get
        {
            return this.diversionStatusField;
        }
        set
        {
            this.diversionStatusField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string DiversionDestination
    {
        get
        {
            return this.diversionDestinationField;
        }
        set
        {
            this.diversionDestinationField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte OldMessageCount
    {
        get
        {
            return this.oldMessageCountField;
        }
        set
        {
            this.oldMessageCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte NewMessageCount
    {
        get
        {
            return this.newMessageCountField;
        }
        set
        {
            this.newMessageCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}


}

调用Web服务的方法:

public ResultUserStatus UserStatus(string userName)
    {
        string req = "req=userStatus";
        string user = "user=" + userName;
       //UserStatusResult result;
        using (WebClient proxy = new WebClient())
        {
            proxy.Headers["Authorization"] = "Basic " +   
            Convert.ToBase64String(Encoding.Default.GetBytes("br:br"));
            proxy.Headers.Add(HttpRequestHeader.Accept, "application/xml");

            try
            {
                var response = proxy.DownloadString(webServiceBasePath + req + "&" + user);


                var rootAttribute = new XmlRootAttribute();
                rootAttribute.IsNullable = true;

                var serialize = new XmlSerializer(typeof (ResultUserStatus), rootAttribute);

                object res;

                using (TextReader read = new StringReader(response))
                {
                   res = serialize.Deserialize(read);
                }                   

                return (ResultUserStatus)res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

此行的异常中断

res = serialize.Deserialize(read);

错误:

  

没想到。

     

[InvalidOperationException:不是预期的。]

     

Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderResultUserStatus.Read3_ResultUserStatus()+262

有没有人知道这可能是什么?

0 个答案:

没有答案