Deserialise a DOM XmlElement to .NET objects
I have an existing application which manipulates data as an XML DOM model. I now want to hand that data off to another subsystem as a collection of C# objects.
As far as I can tell, I could serialise the DOM representation to a MemoryStream and the deserialise the stream using an XmlSerializer. Surely there is a way of processing the DOM representation directly, without the intervening textual representation? But I can't find it.
How can I use System.Xm开发者_StackOverflowl.Serialization on a DOM representation?
It sounds like you want to project an XmlDocument into a custom collection. You can do this with XmlSerializer.Deserialize but it requires that your XmlDocument have its structure lined up nicely with the collection members.
If this is true then using XmlSerializer.Deserialize Method
is a perfectly fine approach using an XmlNodeReader
.
e .g.
XmlDocument xdoc = GetAnXmlDocument();
XmlSerializer serializer = new XmlSerializer(typeof(SomeCollection));
SomeCollection coll = xSerializer.Deserialize(new XmlNodeReader(xdoc));
However if you find that the XmlDocument doesn't line up with your collection you might just want to use Linq-to-xml and do what ever projection you like.
精彩评论