hwo to convert value which get from xml to type which get from xml also 
Author Message
 hwo to convert value which get from xml to type which get from xml also

i want to add xml file layer between the
form and the ado.net .
it like that
 in the form I have a text box which name is text1 ,and value is "23"
and corresponding Field name is Field1 and it's type is int

other  text box which name is text2 ,and value is ""
and corresponding Field name is Field2 and it's type is long
etc.

then i write to xml node
  <xmlnode  type ="Int32" TextName="text1" value ="23" fieldname ="Field1"/>
  <xmlnode  type ="Int64" TextName="text2" value ="234" fieldname
="Field2"/>

then I want to write the  value "23"  and the value "234" to to database
record

but the value I get from xml is string ,
and the value I write to database must be int type etc.

how can I realize the convert between the different type dynamic.



Fri, 14 Jan 2005 12:19:20 GMT  
 hwo to convert value which get from xml to type which get from xml also
Qi Yan Lei,

Okay, so you want to convert a string to some primitive type, right?
But, you only know the type at runtime....
Since both the type and the string value come from the xml, did you think
int what type you will store the converted value? I'll be using "object."
I'm not good at ASP.NET, so I cannot claim that this is the exact solution
for what you're trying to achieve.

here'is my idea:

string type = xmlnode.type ; //this can be "Int32"; or "Int64", or "Double",
etc
string value = "23"; //or "345", or "345.5", etc

object convertedValue;

Type type = Type.GetType("System." + type); //get the type "dynamically"
Method parse = type.GetMethod("Parse", new Type[]
{Type.GetType("System.String")); //get the "Parse" method of that dynamic
type which takes a string argument.
object convertedValue = parse.Invoke(null, new object[] {value});

that's it. "convertedValue" is of the desired type. You can test that
because:

"type.IsInstanceOfType(convertedValue)" always returns "true"

Does this work for you?


Quote:
> i want to add xml file layer between the
> form and the ado.net .
> it like that
>  in the form I have a text box which name is text1 ,and value is "23"
> and corresponding Field name is Field1 and it's type is int

> other  text box which name is text2 ,and value is ""
> and corresponding Field name is Field2 and it's type is long
> etc.

> then i write to xml node
>   <xmlnode  type ="Int32" TextName="text1" value ="23" fieldname
="Field1"/>
>   <xmlnode  type ="Int64" TextName="text2" value ="234" fieldname
> ="Field2"/>

> then I want to write the  value "23"  and the value "234" to to database
> record

> but the value I get from xml is string ,
> and the value I write to database must be int type etc.

> how can I realize the convert between the different type dynamic.



Fri, 14 Jan 2005 13:52:45 GMT  
 hwo to convert value which get from xml to type which get from xml also
thanks

Quote:

> Qi Yan Lei,

> Okay, so you want to convert a string to some primitive type, right?
> But, you only know the type at runtime....
> Since both the type and the string value come from the xml, did you think
> int what type you will store the converted value? I'll be using "object."
> I'm not good at ASP.NET, so I cannot claim that this is the exact solution
> for what you're trying to achieve.

> here'is my idea:

> string type = xmlnode.type ; //this can be "Int32"; or "Int64", or
"Double",
> etc
> string value = "23"; //or "345", or "345.5", etc

> object convertedValue;

> Type type = Type.GetType("System." + type); //get the type "dynamically"
> Method parse = type.GetMethod("Parse", new Type[]
> {Type.GetType("System.String")); //get the "Parse" method of that dynamic
> type which takes a string argument.
> object convertedValue = parse.Invoke(null, new object[] {value});

> that's it. "convertedValue" is of the desired type. You can test that
> because:

> "type.IsInstanceOfType(convertedValue)" always returns "true"

> Does this work for you?



> > i want to add xml file layer between the
> > form and the ado.net .
> > it like that
> >  in the form I have a text box which name is text1 ,and value is "23"
> > and corresponding Field name is Field1 and it's type is int

> > other  text box which name is text2 ,and value is ""
> > and corresponding Field name is Field2 and it's type is long
> > etc.

> > then i write to xml node
> >   <xmlnode  type ="Int32" TextName="text1" value ="23" fieldname
> ="Field1"/>
> >   <xmlnode  type ="Int64" TextName="text2" value ="234" fieldname
> > ="Field2"/>

> > then I want to write the  value "23"  and the value "234" to to database
> > record

> > but the value I get from xml is string ,
> > and the value I write to database must be int type etc.

> > how can I realize the convert between the different type dynamic.



Fri, 14 Jan 2005 14:50:27 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. TCP (HTTP) problems... XML file getting over HTTP

2. Getting XML documentation to appear in assembly

3. Getting the return value type from a MethodBase

4. creating xml schema programmatically from an an xml file without using ADO.NET

5. XML DOM and .NET's XML classes

6. XML.NET to XML.ADO26

7. VC++ and XML, I want to retrieve an XML file from a URL

8. VC++ and XML, I want to retrieve an XML file from URL

9. How can get the element value out the XML when it has variable tag name

10. want to get value of XML element at certain level

11. How can i get the values of all elements under certain level in XML tree

12. Find XML value from C#

 

 
Powered by phpBB® Forum Software