473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deserialization

I have an xml file containing some stuff.

I use xsd to generate a schema and again to create classes from the schemas.

added using System, System.IO;
added [Serializable] to all classes
added namespace ROC
added deserialize() method as follows to AWLR2 class:

public AWLR2 deserialize(string xmlString)
{
AWLR2 awlr2;
try
{
StringReader sr = new StringReader(xmlString);
XmlSerializer ser = new XmlSerializer(this.GetType());
awlr2=(AWLR2)ser.Deserialize(sr);
}
catch(Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("BizTalk Redemptions",
ex.ToString());
throw ex;
}

return awlr2;
}

I attempt to deserialize from a string using the same xml with escaped
quotes to prove it all works and we start with the problems.

The escaped text is as follows:

<AWLR2 xmlns=\"http://Schemas_cares.AWLR2\"><D-1A-1 xmlns=\"\"><LL:LincList
xmlns:LL=\"http://Schemas_cares.LincList\"><Row><Col
Index=\"1\">00200170601</Col><Col Index=\"2\">002001706</Col><Col
Index=\"3\"></Col></Row><Row><Col Index=\"1\">00200170601</Col><Col
Index=\"2\">014628102</Col><Col Index=\"3\"></Col></Row><Row><Col
Index=\"1\">03639200702</Col><Col Index=\"2\">026269507</Col><Col
Index=\"3\"></Col></Row><Row><Col Index=\"1\">03306230708</Col><Col
Index=\"2\">026880902</Col><Col Index=\"3\"></Col></Row><Row><Col
Index=\"1\">03306230708</Col><Col Index=\"2\">033062307</Col><Col
Index=\"3\"></Col></Row><Row><Col Index=\"1\">03639200702</Col><Col
Index=\"2\">036502207</Col><Col Index=\"3\"></Col></Row><Row><Col
Index=\"1\">03850780601</Col><Col Index=\"2\">038507806</Col><Col
Index=\"3\"></Col></Row><Row><Col Index=\"1\">03850780601</Col><Col
Index=\"2\">038588908</Col><Col
Index=\"3\"></Col></Row></LL:LincList></D-1A-1></AWLR2>

I call the deserialize() method and it does not throw any exceptions.

I try to reference objects that should have been created on deserialization
and whilst AWLR2 exists, the objects that AWLR2 should have, do not.

My unit test reports the text passed correctly, and the existence of the
awlr2 object.
Console.WriteLine("awlr2 exists: " + (awlr2 != null).ToString()); //OK

but not
Console.WriteLine("Items exists: " + (awlr2.Items != null).ToString());
This throws NullReferenceException.

Any ideas please. I have checked all the attributes created by xsd on the
properties of the classes and they seem fine. Serialization is not a
problem. Just deserialization.

cheers

PeterW
Jan 3 '06 #1
7 1774
Hi PeterW,

I'm trying to make a repro on my machine, so could you please provide me
with the AWLR2 class here?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 4 '06 #2
Kevin

The code generated by xsd.exe is as follows: (my contributions are
namespace, using statements, [Serializable] attributes and deserialize()
method on AWLR2 class.
----------------------

using System;
using System.Xml.Serialization;
using System.IO;

namespace ROC
{
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://Schemas_cares.AWLR2")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://Schemas_cares.AWLR2", IsNullable=false)]
[Serializable]
public class AWLR2
{

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("D-1A-1",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public AWLR2D1A1[] Items;

public AWLR2 deserialize(string xmlString)
{
AWLR2 awlr2;
try
{
StringReader sr = new StringReader(xmlString);
XmlSerializer ser = new XmlSerializer(this.GetType());
awlr2=(AWLR2)ser.Deserialize(sr);
}
catch(Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("BizTalk Redemptions",
ex.ToString());
throw ex;
}

return awlr2;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://Schemas_cares.AWLR2")]
[Serializable]
public class AWLR2D1A1
{

/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Namespa ce="http://Schemas_cares.LincList")]
[System.Xml.Serialization.XmlArrayItemAttribute("Ro w",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified,
Namespace="http://Schemas_cares.LincList", IsNullable=false)]
public LincListRow[] LincList;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://Schemas_cares.LincList")]
[Serializable]
public class LincListRow
{

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Col" ,
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public LincListRowCol[] Col;

/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://Schemas_cares.LincList")]
[Serializable]
public class LincListRowCol
{

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Index;

/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://Schemas_cares.LincList")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://Schemas_cares.LincList", IsNullable=false)]
[Serializable]
public class LincList
{

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Row" ,
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public LincListRow[] Row;
}
}

------------------------------------------------
awlr2.xsd is as follows:

<?xml version="1.0" encoding="utf-16" ?>
- <xs:schema xmlns:mstns="http://Schemas_cares.AWLR2"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:app1="http://Schemas_cares.LincList"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://Schemas_cares.AWLR2" id="AWLR2"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="awlr2_app1.xsd"
namespace="http://Schemas_cares.LincList" />
- <xs:annotation>
- <xs:appinfo>
- <b:references>
<b:reference targetNamespace="http://Schemas_cares.LincList" />
</b:references>
</xs:appinfo>
</xs:annotation>
- <xs:element msdata:IsDataSet="true" msdata:Locale="en-GB" name="AWLR2">
- <xs:complexType>
- <xs:choice maxOccurs="unbounded">
- <xs:element form="unqualified" name="D-1A-1">
- <xs:complexType>
- <xs:sequence>
<xs:element ref="app1:LincList" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
--------------------------------------------------

LincList.xsd is as follows:

<?xml version="1.0" encoding="utf-16" ?>
- <xs:schema xmlns="http://Schemas_cares.LincList"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
elementFormDefault="unqualified"
targetNamespace="http://Schemas_cares.LincList"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="LincList">
- <xs:complexType>
- <xs:sequence>
- <xs:element minOccurs="0" maxOccurs="unbounded" form="unqualified"
name="Row">
- <xs:complexType mixed="true">
- <xs:sequence>
- <xs:element minOccurs="0" maxOccurs="unbounded" form="unqualified"
name="Col">
- <xs:complexType mixed="true">
<xs:attribute form="unqualified" name="Index" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
--
Can't think of anything else you might need to replicate what I have here.

cheers

PeterW
"Kevin Yu [MSFT]" wrote:
Hi PeterW,

I'm trying to make a repro on my machine, so could you please provide me
with the AWLR2 class here?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 4 '06 #3
Hi PeterW,

Thanks for your repro code. Based on my test, the deserialization process
works fine. You can try to modify new XmlSerializer(this.GetType()); into
new XmlSerializer(typeof(AWLR2));

You can also make it a static method like

public static AWLR2 deserialize(string xmlString)

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 5 '06 #4
Thanks Kevin

Problem as always was at the human end of the keyboard! None so blind as
those who will not see.

cheers
--
PeterW
"Kevin Yu [MSFT]" wrote:
Hi PeterW,

Thanks for your repro code. Based on my test, the deserialization process
works fine. You can try to modify new XmlSerializer(this.GetType()); into
new XmlSerializer(typeof(AWLR2));

You can also make it a static method like

public static AWLR2 deserialize(string xmlString)

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 5 '06 #5
You're welcome. So you mean this fixes the problem?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 6 '06 #6
Yes, all sorted now. I was dumb enough to try to use the object I had
instantiated instead of the return value of the deserialize method. As I
said, none so blind as those who will not see. Your comment about
recommending making the method static helped make the penny drop. :-)

cheers
--
PeterW
"Kevin Yu [MSFT]" wrote:
You're welcome. So you mean this fixes the problem?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 8 '06 #7
It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serg | last post by:
Hi. I have a problem with deserialization decimal value. I have a simple class: public class A { public decimal d; } The serialization a value 0.00000001 is OK. The file is
3
by: TEK | last post by:
There seems to be a bug when deserialization some classes in the .NET framework. If you try to deserialize a class that has a base class that holds a struct with a member that is implementing...
2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
1
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
5
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being...
7
by: Andrew | last post by:
Hi, I am using DataContractJsonSerializer to deserialize JSON string in C# objects but I am having a problem. Suppose I have a class: class Item { public ItemId Id { get; set; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.