User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 456,504 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,666 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 1154 | Replies: 1
Reply
Join Date: Dec 2006
Posts: 31
Reputation: unclepauly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
unclepauly unclepauly is offline Offline
Light Poster

XmlSerializer issue.

  #1  
Sep 28th, 2007
hi,

im using this code to serialize my class into xml:

public static void Save(Class1 myClass)
{
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(@"./myFile.cfg", Encoding.ASCII);
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(myClass.GetType());
x.Serialize(w, myClass);
w.Close();
}

my class has only 2 things to serialize. these are private member variables, which i provide access to via public properties:

/* private vars */
private List<string> _MyStringList = new List<string>();
_MyStringList.Add
private string _MyString;
 
/* public properties */
public string MyString
{
get { return _MyString; }
set { _MyString = value; }
}
public List<string> MyStringList
{
get { return _MyStringList; }
set { _MyStringList= value; }
}

both _MyStringList and _MyString are initialized in the constructor. but only _MyStringList gets serialized. why does the list of strings get seriaslized but not the string?

if i make _MyString (the actual private variable) public, then all works well. but i dont want public members vars.

if i set the private _MyString variable through the public MyString property *before* serializing, then this works too. i would just like to know why i simply cannot intiialize the private member in the constructor and then this value is serialized.

thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 31
Reputation: unclepauly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
unclepauly unclepauly is offline Offline
Light Poster

Re: XmlSerializer issue.

  #2  
Sep 28th, 2007
i think i found the problem, by accident.

originally, the set accessors were commented out in the properties (although they are not in the code above). this causes the value not to be serialized. but when i included the set accessors, it worked fine.

still unsure as to why this though!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 3:36 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC