Click here to Skip to main content
14,697,404 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a console program that reads a XML File and prints some selected value in the console. When i run the program i get an exception Inner Exception 1:
InvalidOperationException: <export_header xmlns="http://www.siriusfs.com/SFI/Export/GL_Export/20051005"> was not expected. for the header tag in the file. HResult=0x80131509
Message=There is an error in XML document (2, 2).
Source=System.Xml


My generated class looks as follows :

[XmlRoot(ElementName = "TRANSACTION")]
   public class TRANSACTION
    {

  public string Correspondence_country { get; set; }
        [XmlAttribute(AttributeName = "term")]
        public string Term { get; set; }
    [XmlAttribute(AttributeName = "mediatype_code")]
        public string mediatype_code { get; set; }
    }

     [XmlRoot(ElementName = "ContractData")]
    public class ContractData
    {
        [XmlElement(ElementName = "TRANSACTION")]
        public List<TRANSACTION> TRANSACTION { get; set; }
    }


Program.cs

static void Main(string[] args)
        {
            Console.WriteLine("\n" + "Reading BidAskCurves XML File");
            Console.WriteLine("===========================================================");
                     
            //get filename        
            string fileName = @"C:\Users\tshumae\Documents\MyProjects\SSPPureSunSystems\Files\GLExport58oct.xml";          
            string path = @"C:\Users\tshumae\Documents\MyProjects\SSPPureSunSystems\Files\";
            string result;
            result = Path.GetFileName(fileName);

            // Create an instance of the XmlSerializer.
            XmlSerializer serializer = new XmlSerializer(typeof(ContractData));

            // Declare an object variable of the type to be deserialized.
            ContractData item;

            using (XmlReader reader = XmlReader.Create(fileName))
            {
                // Call the Deserialize method to restore the object's state.
                item = (ContractData)serializer.Deserialize(reader);

                //Write out the properties of the object. (Visual Only, not needed)
                Console.Write(
                    item.TRANSACTION[0].Term + "\t" +
                    item.TRANSACTION[0].mediatype_code + "\t" );
                   
            }



And the XML schema as as below :
	<?xml version="1.0" encoding="utf-16"?>
<EXPORT_HEADER xmlns="http://www.siriusfs.com/SFI/Export/GL_Export/20051005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.siriusfs.com/SFI/Export/GL_Export/20051005  GL_EXPORT.xsd" date_exported="2019-11-04T08:33:22.697" interface_name="GL_EXPORT" batch_id="212" batch_reference="GLX212" total_transactions="6109" total_amount="0.0000">
<TRANSACTION  mediatype_code="Zimbabwe" term="NB" />
  </TRANSACTION>
  </EXPORT_HEADER>


Any insight on what i might be doing wrong. Ideally the program should print all values.

What I have tried:

I have tried to decorate my root entity with the XmlRoot attribute . The program runs fine but only 1 value of the first row , first column is printed to the console :
[XmlRoot(ElementName = "TRANSACTION")]
   public class TRANSACTION
    {

  public string Term { get; set; }
        [XmlAttribute(AttributeName = "term")]
        public string Term { get; set; }
    [XmlAttribute(AttributeName = "mediatype_code")]
        public string mediatype_code { get; set; }
    }

      [XmlRoot(Namespace = "http://www.siriusfs.com/SFI/Export/GL_Export/20051005", ElementName = "EXPORT_HEADER", DataType = "string", IsNullable = true)]
    // [XmlRoot(ElementName = "ContractData")]
//[XmlRoot(ElementName = "ContractData")]
    public class ContractData
    {
        [XmlElement(ElementName = "TRANSACTION")]
        public List<TRANSACTION> TRANSACTION { get; set; }
    }
Posted
Updated 17-Dec-19 10:24am

There's a syntax error in your XML: the <TRANSACTION> element is self-closing, but it also has a closing tag.
Quote:
The 'EXPORT_HEADER' start tag on line 2 position 2 does not match the end tag of 'TRANSACTION'. Line 4, position 5.
Once that's fixed, your "What I have tried" code works to deserialize the XML correctly. You don't need the DataType and IsNullable attributes on the ContractData's XmlRoot attribute, and you don't need the XmlRoot attribute on the TRANSACTION class; but they don't affect the deserialization.

You're only seeing one value written to the console because there is only one value in your XML, and because you're only displaying the first value. If you want to display all values, you'll need to use a loop.
item = (ContractData)serializer.Deserialize(reader);

foreach (TRANSACTION transaction in item.TRANSACTION)
{
    Console.Write("{0}\t{1}\t", transaction.Term, transaction.mediatype_code);
}
   
v2
Comments
Maciej Los 18-Dec-19 2:13am
   
Hawk eye!
Tshumore 20-Dec-19 5:57am
   
Thanks Richard, regrets a typo when writing the question. I have amended the program as per your comments. I am now getting an exception for 'item' in the foreach loop : "foreach statement cannot operate on variables of type 'ContractData' because 'ContractData' does not contain a public instance definition for 'GetEnumerator'
Richard Deeming 20-Dec-19 7:40am
   
Sorry, that should be:
foreach (TRANSACTION transaction in item.TRANSACTION)
Put a try/catch block around your code so you can see where the exception is being thrown. BTW, it's an Invalid Operation exception.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900