Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Deserialize XML string to Object error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ink - 18 Oct 2007 13:50 GMT
Hi All

I am relatively new to doing this and i think that i am making some king of
school boy error.

The error i am getting is on the following line of code.

XmlSerializer xs = new XmlSerializer(typeof(ImportRecord));

The error is.

"Configuration system failed to initialize"

Below i have attached the class code and the XML i am attempting to work
with.

Please if any one can see what i am doing wrong i would much appreciate a
hand

Thanks
ink

//// Code Start

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace AppConfigTest
{

   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button2_Click(object sender, EventArgs e)
       {
           // Get the configuration file.
           System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

           StringReader stringReader;
           stringReader= new
StringReader(config.GetSection("importFormat").SectionInformation.GetRawXml());
           XmlTextReader xmlReader;
           xmlReader = new XmlTextReader(stringReader);
           XmlSerializer xs = new XmlSerializer(typeof(ImportRecord));

           ImportRecord importFormat =
(ImportRecord)xs.Deserialize(xmlReader);

       }

   }

   [Serializable]
   [XmlType("importRecord")]
   public class ImportRecord
   {

       [XmlAttribute("TableName")]
       public string TableName;

       [XmlAttribute("RowIdentifier")]
       public string RowIdentifier;

       [XmlAttribute("IndexOfInsertion")]
       public string IndexOfInsertion;

       [XmlAttribute("NoOfRowsPerRecord")]
       public string NoOfRowsPerRecord;

       [XmlAttribute("HeaderRowIdentifier")]
       public string HeaderRowIdentifier;

       [XmlElement("fieldData")]
       public FieldData FieldData = new FieldData();

   }

   [Serializable]
   [XmlType("fieldData")]
   public class FieldData
   {
       [XmlAttribute("ColumnName")]
       public string ColumnName;

       [XmlAttribute("FieldName")]
       public string FieldName;

       [XmlAttribute("ColumnIndex")]
       public string ColumnIndex;

       [XmlAttribute("RowIndex")]
       public string RowIndex;

       [XmlAttribute("Required")]
       public Boolean Required;

   }

}

//// Code End

//// XML String start

<importFormat>

 <importRecord TableName="GoodsExpected" RowIdentifier="H"
IndexOfInsertion="0" NoOfRowsPerRecord="5" HeaderRowIdentifier="">

  <fieldData ColumnName="STORAGE PROVIDER" FieldName="CompanyID"
ColumnIndex="2" RowIndex="0"  Required="True" />
  <fieldData ColumnName="WAREHOUSE" FieldName="WarehouseID" ColumnIndex="2"
RowIndex="1"  Required="True" />
  <fieldData ColumnName="ASN REFERENCE" FieldName="ReferenceID"
ColumnIndex="2" RowIndex="2"  Required="True" />
  <fieldData ColumnName="EXPECTED DATE" FieldName="ExpectedDate"
ColumnIndex="2" RowIndex="3"  Required="True" />
  <fieldData ColumnName="CUSTOMERID" FieldName="SupplierID" ColumnIndex="2"
RowIndex="4"  Required="True" />

 </importRecord>

 <importRecord TableName="GoodsReceivedBatch" RowIdentifier="D"
IndexOfInsertion="1" NoOfRowsPerRecord="1" HeaderRowIdentifier="H">

  <fieldData ColumnName="PALLET ID" FieldName="PalletID" ColumnIndex="1"
RowIndex="0"  Required="False" />
  <fieldData ColumnName="ITEM CODE" FieldName="ItemID" ColumnIndex="2"
RowIndex="0"  Required="True" />
  <fieldData ColumnName="QTY" FieldName="Qty" ColumnIndex="3" RowIndex="0"
Required="True" />
  <fieldData ColumnName="ITEM DESCRIPTION" FieldName="Description"
ColumnIndex="4" RowIndex="0"  Required="False" />

 </importRecord>

</importFormat>

//// XML String END
Jon Skeet [C# MVP] - 18 Oct 2007 14:43 GMT
> I am relatively new to doing this and i think that i am making some king of
> school boy error.
[quoted text clipped - 6 lines]
>
> "Configuration system failed to initialize"

That sounds unlikely. It seems much more likely that it's *actually*
failing on this line:

System.Configuration.Configuration config =

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Could you check this carefully, preferrably in the debugger, with a
fresh build, stepping through the code one line at a time and seeing
if it genuinely gets to the XmlSerializer constructor line?

Jon
ink - 18 Oct 2007 14:57 GMT
Hi Jon

I have commented out everything except this line.

XmlSerializer xs = new XmlSerializer(typeof(ImportRecord));

and still it fails.

ink

>> I am relatively new to doing this and i think that i am making some king
>> of
[quoted text clipped - 20 lines]
>
> Jon
Marc Gravell - 18 Oct 2007 15:10 GMT
is your app.config / web.config file valid? missing section handlers,
etc - try removing (or renaming) this file...

Marc
ink - 18 Oct 2007 15:10 GMT
There was a funny Char in my app.config file so you were correct.

>> I am relatively new to doing this and i think that i am making some king
>> of
[quoted text clipped - 20 lines]
>
> Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.