Search code examples
c#exceptiondeserializationxml-namespaces

"xmlns=''> was not expected" while deserializing to object (on some computers)


I am trying to deserialize XML. I have no problems on my system either in dev env. or runtime. My QA analyst has no problems, either. However, at the client site, we are getting the following error:

There is an error in the XML document. System.InvalidOperationException: was not expected. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCrashEntity.Read74_CrashEntity() at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at Utility.XML.XMLUtility.DeserializeObject(String XML, Type ObjectType) at DirectoryFramework.ImportData()

What could be causing this on the client site but not in dev/qa environment?

The source XML (small snippet):

<CitationEntity>
  <CitationNumber>E00C000006</CitationNumber>
  <Agency>123456789</Agency>
  <CaseNumber>1234456</CaseNumber>
</CitationEntity>

Deserialization method:

Type entListType = typeof(List<>).MakeGenericType(entType);
object entities = null;
try
{
    entities = XMLUtility.DeserializeObject(outputXml, entListType);
}
catch (Exception ex)
{...}


public static Object DeserializeObject(String XML, Type ObjectType)
{
    Object retval = null;
    XmlSerializer serializer = new XmlSerializer(ObjectType);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(XML);
    XmlNode node = doc.DocumentElement;
    XmlReader readnode = new XmlNodeReader(node);
    retval = serializer.Deserialize(readnode);

    return retval;
}

Target class snippet:

[DataContract, Serializable]
[ModuleCode("TC")]
[InvolvementDate("CitationDate")]
[MFRStatusHistoryModuleCode("TC")]
public partial class CitationEntity : Entities.baseEntity, INameEntity
{
    [DataMember]
    [DefaultValue("")]
    public string Agency
    {
        get { return _Agency; }
        set
        {
            if (value != _Agency)
            {
                _Agency = value;
                OnPropertyChanged("Agency");
            }
        }
    }
    string _Agency;

    [DataMember]
    [DefaultValue("")]
    public string CaseNumber
    {
        get { return _CaseNumber; }
        set
        {
            if (value != _CaseNumber)
            {
                _CaseNumber = value;
                OnPropertyChanged("CaseNumber");
            }
        }
    }
    string _CaseNumber;

    [DataMember]
    [DefaultValue("")]
    public string CitationNumber
    {
        get { return _CitationNumber; }
        set
        {
            if (value != _CitationNumber)
            {
                _CitationNumber = value;
                OnPropertyChanged("CitationNumber");
            }
        }
    }
    string _CitationNumber;
}

I had .net Framework 4.7 installed on my computer and the client computer had 4.0 or so. I thought that might be the issue. We updated the client computer to 4.8 and it did not make a difference. I updated to 4.8 and it still works as it should.


Solution

  • Sorry for the false alarm on this. It has come to my attention that the target entity was incorrectly chosen as part of the installation of the product. After I went over the installation configuration with the installer line by line, I noticed that the entity was for Crash, not Incident. Once the correct entity was set, the deserialization worked like a charm.

    Lesson learned...Don't assume that someone else installed the product correctly when there is an unexpected error.