By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Deserialize XML string to Object error

P: n/a
ink

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(Configur ationUserLevel.None);

StringReader stringReader;
stringReader= new
StringReader(config.GetSection("importFormat").Sec tionInformation.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

Oct 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Oct 18, 1:50 pm, "ink" <i...@notmyemail.comwrote:
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"
That sounds unlikely. It seems much more likely that it's *actually*
failing on this line:

System.Configuration.Configuration config =

ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.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

Oct 18 '07 #2

P: n/a
ink
Hi Jon

I have commented out everything except this line.

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

and still it fails.

ink


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
On Oct 18, 1:50 pm, "ink" <i...@notmyemail.comwrote:
>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"

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

System.Configuration.Configuration config =

ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.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
Oct 18 '07 #3

P: n/a
is your app.config / web.config file valid? missing section handlers,
etc - try removing (or renaming) this file...

Marc
Oct 18 '07 #4

P: n/a
ink
There was a funny Char in my app.config file so you were correct.


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
On Oct 18, 1:50 pm, "ink" <i...@notmyemail.comwrote:
>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"

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

System.Configuration.Configuration config =

ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.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
Oct 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.