why are you doing this? Why do you want xsi:type attributes? Seems
broken and old-think.
This is not the solution you seek, but it may be useful:
using System.IO;
using System.Xml.Serialization;
namespace Ionic {
public class XmlTextWriterFormattedNoDeclaration :
System.Xml.XmlTextWriter {
public XmlTextWriterFormattedNoDeclaration (System.IO.TextWriter w) :
base(w) { Formatting= System.Xml.Formatting.Indented;}
public override void WriteStartDocument () { }
}
public class Setting
{
[XmlAttribute("name")]
public string setting;
[XmlAttribute("type")]
public string type {
get { return _value.GetType().ToString(); }
set {}
}
[XmlText] // used to emit the value
public string _textValue {
get { return _value.ToString(); }
set { }
}
[XmlIgnore]
object _value; // actually holds the value
public Setting() {}
public Setting(string k, object v)
{
setting = k;
_value = v;
}
}
public class TestDriver {
static void Main(string[] args) {
try {
System.Console.WriteLine("\n====================== ======================\n");
XmlSerializer s1 = new XmlSerializer(typeof(Setting));
// suppress default namespace entries in the root elt
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );
Setting instance;
instance= new Setting("one", "blue");
s1.Serialize(new
XmlTextWriterFormattedNoDeclaration(System.Console .Out), instance, ns);
System.Console.WriteLine("\n");
instance= new Setting("two", 2);
s1.Serialize(new
XmlTextWriterFormattedNoDeclaration(System.Console .Out), instance, ns);
System.Console.WriteLine("\n");
instance= new Setting("three", System.DateTime.Now);
s1.Serialize(new
XmlTextWriterFormattedNoDeclaration(System.Console .Out), instance, ns);
System.Console.WriteLine("\n");
}
catch (System.Exception e1) {
System.Console.WriteLine("Exception!\n" + e1);
}
}
}
}
"Nicki Carstensen" <ni***@FORDbigfoot.com> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
hi all,
I have a class
public class Setting
{
[XmlAttribute("name")]
public string setting;
[XmlElement]
public object value;
public Setting(string k, string v)
{
setting = k;
value = v;
} }
It is serialized like this:
<setting name="somesettingname">
<value xsi:type="xsd:string">2</value>
</setting>
Is there a way to serialize it like so:
<setting name="somesettingname" xsi:type="xsd:int">2</setting>
and
<setting name="somesettingname" xsi:type="xsd:string">hello</setting>
for any primitive type?
Thanks in advance...
regards
Nicki Carstensen
(remove the car)