Got error while reading XML file, XML Document (2,2) contains an error

Rinaldo 396 Reputation points
2021-11-24T19:03:28.583+00:00

I created and CS file with XSD.exe with the help of volenteer @Petrus 【KIM】 but now i get the mentioned error at the beginning XML Document (2,2) contains an error. I don't know what this error is for.
The code:

 static XMLInvoice invoice(string file)  
        {  
            XMLInvoice i ;  
  
            string path = file;  
  
            XmlSerializer serializer = new XmlSerializer(typeof(XMLInvoice));  
  
            FileStream fs = new FileStream(file, FileMode.Open);  
            i = (XMLInvoice)serializer.Deserialize(fs);  
            fs.Close();  
            return i;  
}  

Hopefully somebody can help

Developer technologies C#
{count} votes

Accepted answer
  1. Jose Zero 576 Reputation points
    2021-11-24T23:07:59.05+00:00

    For demonstration I shorten your XML sample to, (save it to a file on disc)

    <?xml version="1.0" encoding="UTF-8"?>
    <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
      <cbc:ID>03000</cbc:ID>
      <cbc:IssueDate>2021-11-18</cbc:IssueDate>
      <cbc:Note>Klant 6176 Factuur 03000</cbc:Note>
    </Invoice>
    

    Class file for XML above will be (Class with Nested Partial Class as you already have)

    public class XMLInvoice
      {
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2", IsNullable = false)]
        public partial class Invoice
        {
          private ushort idField;
          private System.DateTime issueDateField;
          private string noteField;
    
          [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
          public ushort ID
          {
            get
            {
              return this.idField;
            }
            set
            {
              this.idField = value;
            }
          }
    
          [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2", DataType = "date")]
          public System.DateTime IssueDate
          {
            get
            {
              return this.issueDateField;
            }
            set
            {
              this.issueDateField = value;
            }
          }
    
          [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
          public string Note
          {
            get
            {
              return this.noteField;
            }
            set
            {
              this.noteField = value;
            }
          }
        }
      }
    

    To read XML file and get values, use this

    private void GetXMLData()
      {
        XMLInvoice.Invoice MyData;
        string file = @"<place here full apth and name of XML sample file";
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(XMLInvoice.Invoice));
        using (System.IO.Stream reader = new System.IO.FileStream(file, System.IO.FileMode.Open))
        {
          MyData = (XMLInvoice.Invoice)serializer.Deserialize(reader);
        }
        ushort ID = MyData.ID;
        DateTime IssueDate = MyData.IssueDate;
        string Note = MyData.Note;
      }
    

    Run in Debug.....

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Petrus 【KIM】 546 Reputation points
    2021-11-25T00:37:00.08+00:00

    public class XMLInvoice --> Is right?

    xsd.exe creates the xsd class as root (top level).

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2", IsNullable=false)]
    public partial class Invoice {
    
        private string customizationIDField;
    
        private string profileIDField;
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rinaldo 396 Reputation points
    2021-11-24T19:21:12.093+00:00

    I put a part of the XMLInvoice XML here now.

    <?xml version="1.0" encoding="UTF-8"?>
    <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
      <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
      <cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
      <cbc:ID>03000</cbc:ID>
      <cbc:IssueDate>2021-11-18</cbc:IssueDate>
      <cbc:DueDate>2021-11-25</cbc:DueDate>
      <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
      <cbc:Note>Klant 6176 Factuur 03000</cbc:Note>
      <cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
      <cac:OrderReference>
    

  3. Rinaldo 396 Reputation points
    2021-11-24T19:32:16.43+00:00

    Class file:

    using System.Xml.Serialization;
    
    // 
    // This source code was auto-generated by xsd, Version=4.6.1055.0.
    // 
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,
        Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")]
    [System.Xml.Serialization.XmlRootAttribute(
        Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2", IsNullable = false)]
    public class XMLInvoice
    {
        public partial class Party
        {
    
            private PartyPartyName[] partyNameField;
    
            private PartyPostalAddress[] postalAddressField;
    
            private PartyPartyTaxScheme[] partyTaxSchemeField;
    
            private PartyPartyLegalEntity[] partyLegalEntityField;
    
            private PartyContact[] contactField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("PartyName")]
            public PartyPartyName[] PartyName
            {
                get { return this.partyNameField; }
                set { this.partyNameField = value; }
            }
    
    0 comments No comments

  4. Jose Zero 576 Reputation points
    2021-11-24T21:14:25.97+00:00

    By the parts of code, I can only say they not relate each other.
    Inner Exception message is telling you where your problem is

    System.InvalidOperationException: <Invoice xmlns='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'> is not expected

    Usually I read "XML Document (2,2) contains an error" as Line 2, Char2

    Sounds you trying Deserialize right file with incorrect class

    Just in case you can also Generate a Class from XML in VS using Edit > Paste Special > Paste XML as Classes


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.