1
votes

I am receiving the following error trying to deserialize xml. This produces the error:

XmlSerializer serializer = new XmlSerializer(typeof(PrivateOptionsAPIResponse));
var result = serializer.Deserialize(streamReader);

Exception:

System.InvalidOperationException was caught
Message=There is an error in XML document (0, 0)
InnerException: System.Xml.XmlException
Message=Root element is missing
Source=System.Xml

I am not sure how to correct the problem. Request returns the following XML:

<PrivateOptionsAPIResponse> <CountiesForPostalCodeResponse> <Counties> <County> <CountyName>PRINCE WILLIAM</CountyName> <StateCode>VA</StateCode> </County> <County> <CountyName>MANASSAS CITY</CountyName> <StateCode>VA</StateCode> </County> <County> <CountyName>MANASSAS PARK CITY</CountyName> <StateCode>VA</StateCode> </County> </Counties> </CountiesForPostalCodeResponse> </PrivateOptionsAPIResponse>

I used xsd.exe to generate a class. The definition on PrivateOptionsAPIResponse (generated by xsd.exe tool) shows:

public partial class PrivateOptionsAPIResponse {

private object itemField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("CountiesForPostalCodeResponse", typeof(ZipCodeValidationResponse))]
[System.Xml.Serialization.XmlElementAttribute("PlanDetailsForIndividualOrFamilyResponse", typeof(IndividualPlanBenefitResponse))]
[System.Xml.Serialization.XmlElementAttribute("PlansForIndividualOrFamilyResponse", typeof(IndividualPlanQuoteResponse))]
[System.Xml.Serialization.XmlElementAttribute("ProductDetailsForSmallGroupResponse", typeof(SmallGroupProductBenefitResponse))]
[System.Xml.Serialization.XmlElementAttribute("ProductsForSmallGroupResponse", typeof(SmallGroupProductQuoteResponse))]
public object Item {
    get {
        return this.itemField;
    }
    set {
        this.itemField = value;
    }
}

}

If I then browse to ZipCodeValidationResponse definition it shows this:

public partial class ZipCodeValidationResponse {

private CountyType[] countiesField;

/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("County", IsNullable=false)]
public CountyType[] Counties {
    get {
        return this.countiesField;
    }
    set {
        this.countiesField = value;
    }
}

}

If I then browse definition on CountyType I see this:

public partial class CountyType {

private string countyNameField;

private StateAbbreviationType stateCodeField;

/// <remarks/>
public string CountyName {
    get {
        return this.countyNameField;
    }
    set {
        this.countyNameField = value;
    }
}

/// <remarks/>
public StateAbbreviationType StateCode {
    get {
        return this.stateCodeField;
    }
    set {
        this.stateCodeField = value;
    }
}

}

----------Working solution----------------:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            string status = ((HttpWebResponse)response).StatusDescription;

            if(status == "OK")
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        var xmlSerializer = new XmlSerializer(typeof(PrivateOptionsAPIResponse));
                        var privateOptionsAPIResponse = xmlSerializer.Deserialize(reader) as PrivateOptionsAPIResponse;
                    }
                }                    
            }
        }
2
Assuming the reader content is verified, You might have to force the reader to it's content position with "streamReader.MoveToContent()". - Marvin Smit

2 Answers

1
votes

How are you declaring your streamReader? Take a look at its contents and you'll most likely see it's empty or doesn't contain a complete XML document.

0
votes

First, check whether your XML is in valid form. From the given exception, it seems you are supplying an invalid xml document. Much appreciate if you could post the content (xml) that you are trying to de serialize.