473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

XmlSerializer and object typeinfo

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)
Nov 12 '05 #1
1 1783
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)

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Zion Zadik | last post by:
Dear all, I have a set of c# data classes which i need to fill their data from xml files. serialization looks to be the best way to accomplish this task. Since the data classes are compiled and...
5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
8
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
4
by: Ultrakorne | last post by:
hi, i have some problems with my client talk to my server... i am using xmlserializer to serialize object and send them to the other side of the connection. I need to send / recive by both client...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
2
by: christopher.watford | last post by:
I'm loading a plugin assembly using Activator.CreateInstanceFrom, and inside this assembly is a settings class which gets serialized to XML. The general code flow is as follows: ObjectHandle...
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.