locked
XML Deserializing "Element was not expected" error

    Pregunta

  • I know this has been asked before, but I checked the other threads and none were of help to me. I am trying to deserialize an xml to an object and am getting the error:

        "<doPublish xmlns='http://usdoj.gov/leisp/lexs/publishdiscover/3.1'> 
           was not expected."}

    My XML looks like:

    <lexspd:doPublish xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xsi:schemaLocation="http://usdoj.gov/leisp/lexs/publishdiscover/3.1 ../xsd/lexs/publish-discover/3.1/publish-discover.xsd"
        	xmlns:lexspd="http://usdoj.gov/leisp/lexs/publishdiscover/3.1"
        	xmlns:lexs="http://usdoj.gov/leisp/lexs/3.1">
        	<lexs:PublishMessageContainer>
        	    <lexs:PublishMessage>
                    <lexs:PDMessageMetadata>
                    </lexs:PDMessageMetadata>
                </lexs:PublishMessage>
            </lexs:PublishMessageContainer>
        </lexspd:doPublish>
    
    The code I am using to deserialize with looks like:

    XmlSerializer xs = new XmlSerializer(typeof(PublishMessageType));
        Encoding encode = new UTF8Encoding();
    
        PDWebService lexpdServiceProxy = new PDWebService();
        lexpdServiceProxy.Url = "http://59.60.72.12/";
    
        String pdMessageXml = File.ReadAllText(fileName);
    
        DoPublishType doPublishType = new DoPublishType();
        MemoryStream publishMsgMemStream = new MemoryStream(encode.GetBytes(pdMessageXml));
        doPublishType.PublishMessageContainer = new PublishMessageType[1];
        doPublishType.PublishMessageContainer[0] =
           (PublishMessageType)xs.Deserialize(publishMsgMemStream);
    And the object I am trying to deserialize too looks like: (shortened version)

    /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Web.Services.WebServiceBindingAttribute(Name = "PDWebServiceSoapBinding", Namespace = "http://usdoj.gov/leisp/lexs/publishdiscover/3.1/ws")]
        [System.Xml.Serialization.XmlIncludeAttribute(typeof(SRMessageType))]
        [System.Xml.Serialization.XmlIncludeAttribute(typeof(AugmentationType))]
        [System.Xml.Serialization.XmlIncludeAttribute(typeof(PayloadObjectReferenceType))]
        [System.Xml.Serialization.XmlIncludeAttribute(typeof(ComplexObjectType))]
        [System.Xml.Serialization.XmlIncludeAttribute(typeof(MetadataType))]
        public partial class PDWebService : System.Web.Services.Protocols.SoapHttpClientProtocol
        {
    
            private System.Threading.SendOrPostCallback doPublishOperationCompleted;
    
            /// <remarks/>
            public PDWebService()
            {
                this.Url = "http://localhost:9080/PDWebService/services/PDWebServiceBean";
            }
    
            /// <remarks/>
            public event doPublishCompletedEventHandler doPublishCompleted;
    
            /// <remarks/>
            [System.Web.Services.Protocols.SoapDocumentMethodAttribute("urn:#doPublish", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)]
            [return: System.Xml.Serialization.XmlElementAttribute("doPublishReponse", Namespace = "http://usdoj.gov/leisp/lexs/publishdiscover/3.1/ws")]
            public doPublishReponse doPublish([System.Xml.Serialization.XmlElementAttribute("doPublish", Namespace = "http://usdoj.gov/leisp/lexs/publishdiscover/3.1")] DoPublishType doPublish1)
            {
                object[] results = this.Invoke("doPublish", new object[] {
                        doPublish1});
                return ((doPublishReponse)(results[0]));
            }
    
            /// <remarks/>
            public System.IAsyncResult BegindoPublish(DoPublishType doPublish1, System.AsyncCallback callback, object asyncState)
            {
                return this.BeginInvoke("doPublish", new object[] {
                        doPublish1}, callback, asyncState);
            }
    
            /// <remarks/>
            public doPublishReponse EnddoPublish(System.IAsyncResult asyncResult)
            {
                object[] results = this.EndInvoke(asyncResult);
                return ((doPublishReponse)(results[0]));
            }
    
            /// <remarks/>
            public void doPublishAsync(DoPublishType doPublish1)
            {
                this.doPublishAsync(doPublish1, null);
            }
    
            /// <remarks/>
            public void doPublishAsync(DoPublishType doPublish1, object userState)
            {
                if ((this.doPublishOperationCompleted == null))
                {
                    this.doPublishOperationCompleted = new System.Threading.SendOrPostCallback(this.OndoPublishOperationCompleted);
                }
                this.InvokeAsync("doPublish", new object[] {
                        doPublish1}, this.doPublishOperationCompleted, userState);
            }
    
            private void OndoPublishOperationCompleted(object arg)
            {
                if ((this.doPublishCompleted != null))
                {
                    System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                    this.doPublishCompleted(this, new doPublishCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
                }
            }
    
            /// <remarks/>
            public new void CancelAsync(object userState)
            {
                base.CancelAsync(userState);
            }
        }

    I have tried adding:

        [System.Xml.Serialization.XmlRoot("doPublish", Namespace = http://usdoj.gov/leisp/lexs/publishdiscover/3.1", IsNullable = true)]
    To the top of the class I am trying to deserialize too, with no luck. 
    I have also tried changing the XmlSerializer object to:

    XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "doPublish";
        xRoot.Namespace = "http://usdoj.gov/leisp/lexs/publishdiscover/3.1";
        xRoot.IsNullable = true;
        XmlSerializer xs = new XmlSerializer(typeof(PublishMessageType), xRoot);

    This would work, but every type in the deserialized object would return null, even though they are all populated in the xml document.

    Any help would be great, thanks!

    viernes, 06 de abril de 2012 16:05

Todas las respuestas

  • Hello,

    Check out the threads below for the same issue . It may help you.

    http://stackoverflow.com/questions/2504569/element-was-not-expected-while-deserializing-an-array-with-xml-serialization

    http://stackoverflow.com/questions/2797763/xmlns-was-not-expected-when-deserializing-nested-classes

    lunes, 16 de abril de 2012 8:38