J
John A Grandy
I wrote a simple function to serialize types. I create a simple struct,
marking its public properties with [XmlAttribute]. When I serialize this
struct, none of the properties are present as attributes in the xml stream.
public static string XmlSerialize( object target )
{
XmlSerializer serializer = new XmlSerializer( target.GetType() );
StringBuilder xml = new StringBuilder();
StringWriter writer = new StringWriter( xml );
serializer.Serialize( writer, target );
writer.Close();
return xml.ToString();
}
public struct MyStruct
{
private int? _id;
private string _name;
[XmlAttribute]
public int? Id
{
get { return _id; }
}
[XmlAttribute]
public string Name
{
get { return _name; }
}
public MyStruct( int id, string name )
{
_id = id;
_name = name;
}
}
MyStruct my = new MyStruct( 7, "test" );
string xml = XmlSerialize( my );
OUTPUT :
<?xml version="1.0" encoding="utf-16"?>
<MyStruct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
marking its public properties with [XmlAttribute]. When I serialize this
struct, none of the properties are present as attributes in the xml stream.
public static string XmlSerialize( object target )
{
XmlSerializer serializer = new XmlSerializer( target.GetType() );
StringBuilder xml = new StringBuilder();
StringWriter writer = new StringWriter( xml );
serializer.Serialize( writer, target );
writer.Close();
return xml.ToString();
}
public struct MyStruct
{
private int? _id;
private string _name;
[XmlAttribute]
public int? Id
{
get { return _id; }
}
[XmlAttribute]
public string Name
{
get { return _name; }
}
public MyStruct( int id, string name )
{
_id = id;
_name = name;
}
}
MyStruct my = new MyStruct( 7, "test" );
string xml = XmlSerialize( my );
OUTPUT :
<?xml version="1.0" encoding="utf-16"?>
<MyStruct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />