I did something similar to Andark's answer, except I used the DataContractSerializer instead of XmlSerializer. This was done in VS 2012 targeting .NET 4.5.
Here's the test code:
using Sytem;
using System.IO;
using System.Runtime.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person myPerson = new Person() { Name = "Tim" };
using (FileStream writer = new FileStream("Person.xml", FileMode.Create, FileAccess.Write))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
dcs.WriteObject(writer, myPerson);
}
}
}
[DataContract]
class Person
{
private string m_name;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
}
}
When I run this, I get the following XML:
<Person xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Name>Tim</Name>
<Person>
Which is what is expected.
It's important to note that the default serializer for DataContract is the DataContractSerializer, not XmlSerializer, and there are some differences. Only members that are marked with [DataMember] should be serialized, and the access level (private, public, etc) is irrelevant - if you mark a private field or member with [DataMember], it will be serialized.