Jose Miguel Vega Lopez
Jose Miguel Vega Lopez

Reputation: 119

Cannot Deserialize ECReports in XML - Error: Exception thrown: 'System.InvalidOperationException' in System.Xml.dll

I am working with the ale 1.1 schema. I am trying to extract ECReports from XML like this:

ECReports reports = new ECReports();
string path = @"C:\DevTools\tag1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(ECReports));
StreamReader reader = new StreamReader(path);
reports = (ECReports) serializer.Deserialize(reader);
reader.Close();

I get the following error in line 5: Exception thrown: 'System.InvalidOperationException' in System.Xml.dll

Here is the InnerException message: Didn't expect: <ECReports xmlns='urn:epcglobal:ale:xsd:1'>

The ECReports class has the same namespace of the xml file:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:epcglobal:ale:xsd:1")]

Here is my XML:

<?xml version="1.0"?>
<ECReports xmlns="urn:epcglobal:ale:xsd:1" schemaVersion="1.1"
   creationDate="2008-02-19T10:54:06.444-05:00" specName="ECSpec1"
   date="2008-02-19T10:54:06.444-05:00" ALEID="ALEID_1"
   totalMilliseconds="5000" terminationCondition="DURATION">
   <reports>
      <report reportName="ReportName1">
         <group>
            <groupList>
               <member>
                  <tag>
                     urn:epc:tag:sgtin-96:3.0037000.006542.773346595
                  </tag>     
               </member>
            </groupList>
            <groupCount>
               <count>1</count>
            </groupCount>
         </group>
      </report>
      <report reportName="ReportName2" />
   </reports>
</ECReports>

Upvotes: 0

Views: 144

Answers (2)

User9995555
User9995555

Reputation: 1556

Try this...

Usings...

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;

Classes...(created from your XML using http://xmltocsharp.azurewebsites.net/)

[XmlRoot(ElementName = "member", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Member
{
    [XmlElement(ElementName = "tag", Namespace = "urn:epcglobal:ale:xsd:1")]
    public string Tag { get; set; }
}

[XmlRoot(ElementName = "groupList", Namespace = "urn:epcglobal:ale:xsd:1")]
public class GroupList
{
    [XmlElement(ElementName = "member", Namespace = "urn:epcglobal:ale:xsd:1")]
    public Member Member { get; set; }
}

[XmlRoot(ElementName = "groupCount", Namespace = "urn:epcglobal:ale:xsd:1")]
public class GroupCount
{
    [XmlElement(ElementName = "count", Namespace = "urn:epcglobal:ale:xsd:1")]
    public string Count { get; set; }
}

[XmlRoot(ElementName = "group", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Group
{
    [XmlElement(ElementName = "groupList", Namespace = "urn:epcglobal:ale:xsd:1")]
    public GroupList GroupList { get; set; }
    [XmlElement(ElementName = "groupCount", Namespace = "urn:epcglobal:ale:xsd:1")]
    public GroupCount GroupCount { get; set; }
}

[XmlRoot(ElementName = "report", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Report
{
    [XmlElement(ElementName = "group", Namespace = "urn:epcglobal:ale:xsd:1")]
    public Group Group { get; set; }
    [XmlAttribute(AttributeName = "reportName")]
    public string ReportName { get; set; }
}

[XmlRoot(ElementName = "reports", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Reports
{
    [XmlElement(ElementName = "report", Namespace = "urn:epcglobal:ale:xsd:1")]
    public List<Report> Report { get; set; }
}

[XmlRoot(ElementName = "ECReports", Namespace = "urn:epcglobal:ale:xsd:1")]
public class ECReports
{
    [XmlElement(ElementName = "reports", Namespace = "urn:epcglobal:ale:xsd:1")]
    public Reports Reports { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
    [XmlAttribute(AttributeName = "schemaVersion")]
    public string SchemaVersion { get; set; }
    [XmlAttribute(AttributeName = "creationDate")]
    public string CreationDate { get; set; }
    [XmlAttribute(AttributeName = "specName")]
    public string SpecName { get; set; }
    [XmlAttribute(AttributeName = "date")]
    public string Date { get; set; }
    [XmlAttribute(AttributeName = "ALEID")]
    public string ALEID { get; set; }
    [XmlAttribute(AttributeName = "totalMilliseconds")]
    public string TotalMilliseconds { get; set; }
    [XmlAttribute(AttributeName = "terminationCondition")]
    public string TerminationCondition { get; set; }
}

Code...

        try
        {
            ECReports deserializedXML = new ECReports();
            // Deserialize to object
            XmlSerializer serializer = new XmlSerializer(typeof(ECReports));
            using (FileStream stream = File.OpenRead(@"xml.xml"))
            {
                deserializedXML = (ECReports)serializer.Deserialize(stream);
            } // Put a break-point here, then mouse-over deserializedXML
        }
        catch (Exception)
        {

            throw;
        }

This code will Deserialize your XML as-is and no errors.... Save your XML to a file (xml.xml) in the same folder as your .EXE.... Hope that helps.

Upvotes: 0

Jose Miguel Vega Lopez
Jose Miguel Vega Lopez

Reputation: 119

I delete: xmlns="urn:epcglobal:ale:xsd:1" from the XML file and now my code works! But I would love to know another approach that do not delete that property to make the code run well

Upvotes: 0

Related Questions