Urgent Help With Serialization needed
-
Hi, I'm new to the world of .Net and C#. I need some help with serialization: I'm trying to serialize an object to an XML file, this object has a nested class which I also want to serialize:
[Serializable]
public class NewOrder
{
//constructor
public NewOrder()
{
}//Destructor ~NewOrder() { } private string accountGroupfield; private string accountNumberfield; public string AccountGroup { get { return accountGroupfield; } set { accountGroupfield = value; } } public string AccountNumber { get { return accountNumberfield; } set { accountNumberfield = value; } }
[Serializable]
public class CardDetail
{private string cardFirstNamefield; private string cardLastNamefield; private string cardNumberfield; public string CardFirstName { get { return cardFirstNamefield; } set { cardFirstNamefield = value; } } public string CardLastName { get { return cardLastNamefield; } set { cardLastNamefield = value; } } public string CardNumber { get { return cardNumberfield; } set { cardNumberfield = value; } }
}
I have a function to serialize the object but all I get in the Xml file is:
<?xml version="1.0"?>
<NewOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccountGroup>100</AccountGroup>
<AccountNumber>66302</AccountNumber>
</NewOrder>I've been searching around the internet for days now, but I still havent managed to get the nested class output
-
Hi, I'm new to the world of .Net and C#. I need some help with serialization: I'm trying to serialize an object to an XML file, this object has a nested class which I also want to serialize:
[Serializable]
public class NewOrder
{
//constructor
public NewOrder()
{
}//Destructor ~NewOrder() { } private string accountGroupfield; private string accountNumberfield; public string AccountGroup { get { return accountGroupfield; } set { accountGroupfield = value; } } public string AccountNumber { get { return accountNumberfield; } set { accountNumberfield = value; } }
[Serializable]
public class CardDetail
{private string cardFirstNamefield; private string cardLastNamefield; private string cardNumberfield; public string CardFirstName { get { return cardFirstNamefield; } set { cardFirstNamefield = value; } } public string CardLastName { get { return cardLastNamefield; } set { cardLastNamefield = value; } } public string CardNumber { get { return cardNumberfield; } set { cardNumberfield = value; } }
}
I have a function to serialize the object but all I get in the Xml file is:
<?xml version="1.0"?>
<NewOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccountGroup>100</AccountGroup>
<AccountNumber>66302</AccountNumber>
</NewOrder>I've been searching around the internet for days now, but I still havent managed to get the nested class output
Well as far as I can see, you never declare an instance of
CardDetail
. Serialization only serializes instances of objects, not declarations of objects. BTW, unless there is something in the part of your code that you haven't posted, I see no reason forCardDetail
to be a nested class.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Well as far as I can see, you never declare an instance of
CardDetail
. Serialization only serializes instances of objects, not declarations of objects. BTW, unless there is something in the part of your code that you haven't posted, I see no reason forCardDetail
to be a nested class.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Could you provide some sample code of how i do that, and where i put the code? would it be similar to:
public CardDetail MyCardDetail;
before i declare the class? Thanks in advance! George
Yes but you will need to instantiate it and give its properties some values. Add the declaration as you have suggested (I would put it with the other two field declarations). Also, strictly speaking you should make it private, then add a public property for access from outside the class. Then modify your constructor for NewOrder, something like:
//constructor public NewOrder() { MyCardDetail = new CardDetail(); MyCardDetail.CardFirstName = "Horace"; MyCardDetail.CardLastName = "Horsecollar"; MyCardDetail.CardNumber = "1ABC23"; }
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Yes but you will need to instantiate it and give its properties some values. Add the declaration as you have suggested (I would put it with the other two field declarations). Also, strictly speaking you should make it private, then add a public property for access from outside the class. Then modify your constructor for NewOrder, something like:
//constructor public NewOrder() { MyCardDetail = new CardDetail(); MyCardDetail.CardFirstName = "Horace"; MyCardDetail.CardLastName = "Horsecollar"; MyCardDetail.CardNumber = "1ABC23"; }
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Hi Henry, thanks for your help, I updated the project with your comments but now I'm getting a HTTP 500 error when i Invoke the web service, the full code listing is below: order.aspx.cs
using System;
using System.IO;
using System.Xml.Serialization;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using ExampleforCodeProject;namespace ExampleforCodeProject
{\[WebService(Namespace = "http://MyXmlTest/")\] \[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1\_1)\] \[ToolboxItem(false)\] public class Order : System.Web.Services.WebService { \[WebMethod\] public NewOrder SerialiseOrder() { NewOrder order1 = new NewOrder(); order1.AccountGroup = "11"; order1.AccountNumber = "878"; order1.Card.CardFirstName = "George"; order1.Card.CardLastName = "Balden"; order1.Card.CardNumber = "872947924793274"; Stream stream = File.Open("C:\\\\test\\\\NewSoap.xml", FileMode.Create); //Create a file XmlSerializer sf = new XmlSerializer(typeof(NewOrder)); // sf.Serialize(stream, order1); //serialise stream.Close(); return order1; } }
}
and the Order class (NewOrder.cs):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ExampleforCodeProject;namespace ExampleforCodeProject
{
[Serializable]
public class NewOrder
{public NewOrder() { } //constructor ~NewOrder() { } //Destructor private string accountGroupfield; private string accountNumberfield; public string AccountGroup { get { return accountGroupfield; } set { accountGroupfield = value; } } public string AccountNumber { get { return accountNumberfield; } set { accountNumberfield = value; } } public CardDetail Card; \[Serializable\] public class CardDetail
-
Hi Henry, thanks for your help, I updated the project with your comments but now I'm getting a HTTP 500 error when i Invoke the web service, the full code listing is below: order.aspx.cs
using System;
using System.IO;
using System.Xml.Serialization;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using ExampleforCodeProject;namespace ExampleforCodeProject
{\[WebService(Namespace = "http://MyXmlTest/")\] \[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1\_1)\] \[ToolboxItem(false)\] public class Order : System.Web.Services.WebService { \[WebMethod\] public NewOrder SerialiseOrder() { NewOrder order1 = new NewOrder(); order1.AccountGroup = "11"; order1.AccountNumber = "878"; order1.Card.CardFirstName = "George"; order1.Card.CardLastName = "Balden"; order1.Card.CardNumber = "872947924793274"; Stream stream = File.Open("C:\\\\test\\\\NewSoap.xml", FileMode.Create); //Create a file XmlSerializer sf = new XmlSerializer(typeof(NewOrder)); // sf.Serialize(stream, order1); //serialise stream.Close(); return order1; } }
}
and the Order class (NewOrder.cs):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ExampleforCodeProject;namespace ExampleforCodeProject
{
[Serializable]
public class NewOrder
{public NewOrder() { } //constructor ~NewOrder() { } //Destructor private string accountGroupfield; private string accountNumberfield; public string AccountGroup { get { return accountGroupfield; } set { accountGroupfield = value; } } public string AccountNumber { get { return accountNumberfield; } set { accountNumberfield = value; } } public CardDetail Card; \[Serializable\] public class CardDetail
I do not see a
Card = new CardDetail()
anywhere, you must create an instance before you can use it. I would suggest that you put it in the Constructor for NewOrder since each NewOrder currently requires CardDetail. Later you might want to switch toLazy Initialization
(google it for details). Although it is difficult to see why that would give an HTTP Error, it is necessary, so try it anyway.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”