473,320 Members | 1,978 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,320 software developers and data experts.

XmlConvert and Object type

I'm looking for a way to retrieve data via
System.Reflection.FieldInfo.GetValue (which returns an Object) and convert
it to XML using XmlConvert.ToString.

XmlConvert accepts only specified data types, so I could have a big ugly
branching Select statement to call the correct ToString overload... but
there must be an easier way.

Any suggestions?

Cheers,

Jon
Nov 11 '05 #1
2 2052
XmlConvert cannot convert any arbitrary object to XML. For that you need
the XmlSerializer:

YourObjectType t = ...
XmlSerializer s = new XmlSerializer(typeof(YourObjectType));
s.Serialize(Console.Out, t);

This will give you all the public members. If you want to also serialize
the private state then use the System.Runtime.Serialization with the
System.Runtime.Serialization.Formatters.Soap.SoapF ormatter class.
"Jon the Blind" <no****@spamfree.biz> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
I'm looking for a way to retrieve data via
System.Reflection.FieldInfo.GetValue (which returns an Object) and convert
it to XML using XmlConvert.ToString.

XmlConvert accepts only specified data types, so I could have a big ugly
branching Select statement to call the correct ToString overload... but
there must be an easier way.

Any suggestions?

Cheers,

Jon

Nov 11 '05 #2
Sorry, perhaps I wasn't clear enough about what I need. I'm not looking to
convert complex types, just value types (plus Guids and Strings) passed in a
variable of type Object.

I'm using Reflection to retrieve the values, and I want to insert them into
XML attributes. The safest way to do this is with XmlConvert, but it won't
accept an Object type. I can write a big branching statement for all the
possible actual types of my object (per sample code below), but I was hoping
for something a little more elegant.

Dim xmlString as String
Dim value as Object = CType(myStuffFromReflection,
FieldInfo).GetValue(someObj)

If TypeOf(obj) Is String Then
dim s as String = CType(value, String)
xmlString = XmlConvert.ToString(s) ' ToString only has overloads
for strongly-typed variables
ElseIf TypeOf(obj) Is Integer Then
dim i as Integer = CType(value, Integer)
xmlString = XmlConvert.ToString(i)
' etc...
Any suggestions?

Thanks.

Jon


"Jon the Blind" <no****@spamfree.biz> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
I'm looking for a way to retrieve data via
System.Reflection.FieldInfo.GetValue (which returns an Object) and convert
it to XML using XmlConvert.ToString.

XmlConvert accepts only specified data types, so I could have a big ugly
branching Select statement to call the correct ToString overload... but
there must be an easier way.

Any suggestions?

Cheers,

Jon

Nov 11 '05 #3

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

Similar topics

0
by: Jim M | last post by:
I use EncodeName in my server side ASPX VB code as shown below. xml_qdata = xml_qdata & "<NoteItem>" & System.Xml.XmlConvert.EncodeName(myReader.Item("Note")) & "</NoteItem>" I THEN LOAD...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
2
by: Shawn Anderson | last post by:
I have a question and I think I know the answer, but I am looking for confirmation (and maybe a link to some documentation) I have an input string of: "2004-08-01T00:00:00-00:00" When I pass...
1
by: Chris | last post by:
HI, what is the purpose of XmlConvert.ToString() ? why not just use Convert.ToString() ? e.g. Int32 vInt32 = -2147483648; string s1 = XmlConvert.ToString(vInt32); string s2 =...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
2
by: Soren S. Jorgensen | last post by:
Hi, The docs says (both local and on msdn) that XmlConvert contains methods ToBinHexString and FromBinHexString! But my compiler complains about not finding these methods! Are they not...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
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...
1
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: 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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.