Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Urgent Help With Serialization needed

Urgent Help With Serialization needed

Scheduled Pinned Locked Moved C#
csharpalgorithmsxmljsonhelp
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrgBalden
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • G GrgBalden

      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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      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 for CardDetail 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.”

      G 1 Reply Last reply
      0
      • H Henry Minute

        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 for CardDetail 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.”

        G Offline
        G Offline
        GrgBalden
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • G GrgBalden

          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

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          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.”

          G 1 Reply Last reply
          0
          • H Henry Minute

            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.”

            G Offline
            G Offline
            GrgBalden
            wrote on last edited by
            #5

            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
            
            H 1 Reply Last reply
            0
            • G GrgBalden

              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
              
              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              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 to Lazy 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.”

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups