0

I am trying to deserialize and xml file using an object that was created with xsd.exe tool. When I tried to run the sample app, I am getting the following exception, InvalidOperationException - There is an error in XML document (1, 1). What am I doing wrong here? Here are my codes:

Sample XML File

<?xml version="1.0" encoding="utf-8"?>
<patient>
 <pr>
<fname>Cindy </fname>
<lname>Knapp </lname>
<id>1 </id>
 </pr>
 <pr>
<fname>Regie </fname>
<lname>Miller </lname>
<id>2 </id>
 </pr>
<ad>
  <id>1</id>
  <streetnumber>1200 NW</streetnumber>
  <city>Miami </city>
  <state>FL</state>
  <zip>34488</zip>
</ad>
<ad>
  <id>2</id>
  <streetnumber>1299 NE</streetnumber>
  <city>NY </city>
  <state>NY</state>
  <zip>9032</zip>
</ad>
</patient>

Here is the class that was generated

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18444
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class patient {

    private patientPR[] prField;

    private patientAD[] adField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("pr")]
    public patientPR[] pr {
        get {
            return this.prField;
        }
        set {
            this.prField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ad")]
    public patientAD[] ad {
        get {
            return this.adField;
        }
        set {
            this.adField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class patientPR {

    private string fnameField;

    private string lnameField;

    private int idField;

    /// <remarks/>
    public string fname {
        get {
            return this.fnameField;
        }
        set {
            this.fnameField = value;
        }
    }

    /// <remarks/>
    public string lname {
        get {
            return this.lnameField;
        }
        set {
            this.lnameField = value;
        }
    }

    /// <remarks/>
    public int id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class patientAD {

    private int idField;

    private string streetnumberField;

    private string cityField;

    private string stateField;

    private ushort zipField;

    /// <remarks/>
    public int id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    /// <remarks/>
    public string streetnumber {
        get {
            return this.streetnumberField;
        }
        set {
            this.streetnumberField = value;
        }
    }

    /// <remarks/>
    public string city {
        get {
            return this.cityField;
        }
        set {
            this.cityField = value;
        }
    }

    /// <remarks/>
    public string state {
        get {
            return this.stateField;
        }
        set {
            this.stateField = value;
        }
    }

    /// <remarks/>
    public ushort zip {
        get {
            return this.zipField;
        }
        set {
            this.zipField = value;
        }
    }
}

In the main function, I tried to deserialize the xml file.

 class Program
    {
        static void Main(string[] args)
        {
            patient pr = new patient();
            XmlSerializer serializer = new XmlSerializer(typeof(patient));
            FileStream myStream = new FileStream(@"C:\com\dss\VXReports\XSD Document\EXAMPLE.CS", FileMode.Open);
            pr = (patient)serializer.Deserialize(myStream);
            string kr = string.Empty;
        }
    }

Stack Trace:
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
   at TestXMLFile.Program.Main(String[] args) in c:\com\dss\VXReports\Clinical Quality Measure\TestXMLFile\TestXMLFile\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3802347
  • 108
  • 1
  • 10
  • 2
    "C:\com\dss\VXReports\XSD Document\EXAMPLE.CS" - ".cs" is somewhat unusual extension for an XML file. – Alexei Levenkov Sep 03 '14 at 00:14
  • @AlexeiLevenkov that was the issue. I spend more than 4 hours trying to figure out what was wrong with the code, and I did not even noticed the .cs extension at the end of the file name. Thanks. – user3802347 Sep 03 '14 at 00:58
  • The trick was to pay attention to the exception message. It was not lying to you. Remember, "Once you have eliminated the impossible, whatever remains, however unlikely, is the truth". So, even though your XML document looked perfect, your input to the XML Serializer was not a valid XML document. That implies that your perfect XML document was not the input to the XML Serializer... – John Saunders Sep 03 '14 at 02:03

2 Answers2

1

Your code works perfectly fine for me. Remove "Sample XML File" from the top if you have that in your XML. If you don't have that there, you might have some issue like invisible characters or possibly an encoding conflict. See this link here on SO, for example: Deserialization error in XML document(1,1)

Community
  • 1
  • 1
Matt
  • 6,787
  • 11
  • 65
  • 112
0

I change the file extension from in from .cs to .xml. I also created some partial classes within the project and added some XML Decoration to the classes. Nothing has changed in the class that was automatically generated using xsd.exe. My main Class

 static void Main(string[] args)
        {
            patient pr = new patient();
            XmlSerializer serializer = new XmlSerializer(typeof(patient));
            FileStream myStream = new FileStream(@"C:\com\dss\VXReports\XSD Document\EXAMPLE.XML", FileMode.Open);
            pr = (patient)serializer.Deserialize(myStream);
            string kr = string.Empty;
        }

The new Partial classes:

 [Serializable, XmlRoot("patient")]
    public partial class patient
    {
        [XmlElement("pr")]
        public patientPR[] pr { get; set; }
        [XmlElement("ad")]
        public patientAD[] ad { get; set; }
    }

    [Serializable, XmlType("pr")]
    public partial class patientPR
    {
        [XmlElement("fname")]
        public string fname { get; set; }
        [XmlElement("lname")]
        public string lname { get; set; }
        [XmlElement("id")]
        public int id { get; set; }
    }

    [Serializable, XmlType("ad")]
    public partial class patientAD
    {
        [XmlElement("id")]
        public int id { get; set; }
        [XmlElement("streetnumber")]
        public string streetnumber { get; set; }
        [XmlElement("city")]
        public string city { get; set; }
        [XmlElement("state")]
        public string state { get; set; }
        [XmlElement("zip")]
        public ushort zip { get; set; }
    }
user3802347
  • 108
  • 1
  • 10