Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

In My application, I am Serializing an object into an XML file, while serializing I am not getting any error.

But after serialization completion, then I am checking the XML file it is missing the Properties which are of type boolean and remaining all are in the XML file,

If any one knows the reason, please post your valuable answers.

For example
class abc
{
public int X
{set;
get;
}
public String XY
{set;
get;
}
public bool XB
{set;
get;
}
}

While serializing the above abc class object we are getting the XY,X values but we are not getting the XB in the XML File.

Thanks in advance...

Thanks and Regards
V.S.R.K.Raju...
Posted
Updated 20-Jul-10 21:31pm
v2

Hi SivaRamaKrishnaRaju,

Please find the code below which wioll save the serialised xml to C:\ with boolean value also

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.IO;
using System.Xml;
namespace ConsoleApplication2
{
    public class Program 
    {
        static void Main(string[] args)
        {
            abc xobj = new abc();
            xobj.x = 5;
            xobj.xy = "RCS";
            xobj.xb = false;
            System.Xml.Serialization.XmlSerializer xmlObj = new System.Xml.Serialization.XmlSerializer(xobj.GetType());
            xmlObj.Serialize(Console.Out, xobj);
            Console.WriteLine();    // This will write the output to console
            Console.ReadLine();
            
            TextWriter writer = new StreamWriter(@"C:\\a.xml"); //This will save the serialised xml to a.xml in C:\ drive
            xmlObj.Serialize(writer, xobj);
            writer.Close();         
         }
    }     
    public  class abc
    {
        public int x
        {
            get;
            set;
        }
        public string xy
        {
            get;
            set;
        }
        public bool xb
        {
            get;
            set;
        }
    }
}


Output :

XML
<?xml version="1.0" encoding="utf-8" ?>
- <abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <x>5</x>
  <xy>RCS</xy>
  <xb>false</xb>
  </abc>


Please dont forget to vote if this helps you
 
Share this answer
 
Comments
V.SivaRamaKriShnaRaju 23-Jul-10 4:17am    
Reason for my vote of 3
it is
good
You could ignore the initial bool value and introduce a replacement property used for xml serialization.

C#
[XmlIgnore]
public bool MyValue { get; set; }

/// <summary>Get a value purely for serialization purposes</summary>
[XmlElement("MyValue")]
public string MyValueSerialize
{
    get { return this.MyValue ? "1" : "0" }
    set { this.MyValue = XmlConvert.ToBoolean(value); }
}


Good luck!
 
Share this answer
 
Hi

Thanks for Quick Reply,

But we are given the class as sample but actually the object we serialized is other one.

Actually we are using an web service in this we are sending the Request object and Receiving the Reply object. and we are saving the request object content in XML file while saving we are missing the boolean type property.

the web service is FedEx web service,and i think Web service classes are serialized classes so the Request class is also Serialized one.
 
Share this answer
 
Comments
E.F. Nijboer 21-Jul-10 5:42am    
But if you use "true" and "false" instead of 1 and 0 in the example above, this would be solved. You name the MyValueSerialize as expected by the webservice and internally you can rename and use the MyValue property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900