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. Jose Zero 576 Reputation points
    2021-11-24T23:10:07.277+00:00

    Don´t know why previous comment gets removed, anyway, try sample above

    0 comments No comments

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.