Connecting Tech Pros Worldwide Forums | Help | Site Map

XmlNode -> Schema -> Object

Brad Quinn
Guest
 
Posts: n/a
#1: Nov 11 '05
Lets say I have an XmlNode that validates against some schema. I also have
a class that was generated by the xsd tool using said schema.

What is the best way to get from an XmlNode to an instance of the class?



Dino Chiesa [MSFT]
Guest
 
Posts: n/a
#2: Nov 11 '05

re: XmlNode -> Schema -> Object


System.Xml.Serialization.XmlSerializer.Deserialize ()
accepts an XmlReader.
http://msdn.microsoft.com/library/en...lizeTopic3.asp

and you can create a System.Xml.XmlNodeReader from a System.Xml.XmlNode
http://msdn.microsoft.com/library/en...sctorTopic.asp


"Brad Quinn" <brad_quinn@yahoo.com> wrote in message
news:eF6R4ahVDHA.2248@TK2MSFTNGP10.phx.gbl...[color=blue]
> Lets say I have an XmlNode that validates against some schema. I also[/color]
have[color=blue]
> a class that was generated by the xsd tool using said schema.
>
> What is the best way to get from an XmlNode to an instance of the class?
>
>[/color]


Brad Quinn
Guest
 
Posts: n/a
#3: Nov 11 '05

re: XmlNode -> Schema -> Object


Thanks Dino,

Going the other way (I know I didn't mention this) doesn't seem as elegant.

Is there a better way than this?

// slightly paraphrased
Stream stream = new MemoryStream();
new XmlSerializer( typeof(MyObject) ).Serialize( stream, myObject );
stream.Seek( 0, SeekOrigin.Begin );
XmlDocument doc = new XmlDocument();
doc.Load( stream );
return doc.DocumentElement;

Thanks again,
Brad

"Dino Chiesa [MSFT]" <dinoch@microsoft.com> wrote in message
news:OINWNnhVDHA.2260@TK2MSFTNGP09.phx.gbl...[color=blue]
> System.Xml.Serialization.XmlSerializer.Deserialize ()
> accepts an XmlReader.
>[/color]
http://msdn.microsoft.com/library/en...lizeTopic3.asp[color=blue]
>
> and you can create a System.Xml.XmlNodeReader from a System.Xml.XmlNode
>[/color]
http://msdn.microsoft.com/library/en...sctorTopic.asp[color=blue]
>
>
> "Brad Quinn" <brad_quinn@yahoo.com> wrote in message
> news:eF6R4ahVDHA.2248@TK2MSFTNGP10.phx.gbl...[color=green]
> > Lets say I have an XmlNode that validates against some schema. I also[/color]
> have[color=green]
> > a class that was generated by the xsd tool using said schema.
> >
> > What is the best way to get from an XmlNode to an instance of the class?
> >
> >[/color]
>
>[/color]


Closed Thread