 |
|
|
Hi
Good work Tom.
Some of the public properties like depth, valuepath could not be serialized. Its got no set property in it so i assume they are private fields and so not set but i need these values as i populate the treeview. I hold data in the valuepath which needs to be stored inthe database. Any ideas on this one
Thanks
RainManALex
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
Excellent code, I am using without any problem.
At this moment, I want to return the serialized result from a web service to the clients.
So, I need to save the serialized result into a MemoryStream or Stream or string, or...???? in order to use in a web service function.
The serialization will run in a web service and the deserialization will run in the a client function.
Obviously, I want to deserialize from a MemoryStream or Stream or string, or...??? and load into the client TreeView again.
I was tried the following code to serialize / deserialize.
The DeSerializeTvwFromMemory function return the error: Cannot access a closed Stream.
The SerializeTvwToMemory function do not return errors, but I am not sure that works fine.
Anybody can show me, how I can do it please?
Thanks in advance,
Public Shared Function SerializeTvwToMemory(ByVal treeView As TreeView) As MemoryStream
'Create as serializer and MemoryStream to save TreeViewData Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData)) Dim MyStream As New System.IO.MemoryStream() Dim writer As New System.Xml.XmlTextWriter(MyStream, Nothing)
'Generate TreeViewData from TreeView and serialize the file. ser.Serialize(writer, New TreeViewData(treeView))
'Tidy up writer.Close() MyStream.Close()
Return MyStream
End Function
Public Shared Function DeSerializeTvwFromMemory(ByVal treeView As TreeView, ByVal MySerie As MemoryStream) as boolean
'Create as serializer and get the MemoryStrean to deserialize Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData)) Dim MyStream As New System.IO.MemoryStream() Dim reader As New System.Xml.XmlTextReader(MyStream)
'Deserialize the file and populate the treeview Dim treeData As TreeViewData = CType(ser.Deserialize(reader), TreeViewData) treeData.PopulateTree(treeView)
'Tidy up reader.Close() MyStream.Close() MyStream = Nothing
Return true
End Sub
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
First, let me say that this project is awesome, it was a tremendous help to me and learned a lot from it.
The one other thing I'd like to do is to serialize an array list (of strings) that I store in the TAG property of each node. When I call the SaveTreeViewData method I get an XML file error. Does anybody have any ideas on how I can resolve this?
Roger
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have to thank you. this is very powerful code and worked like a charm. This is by far the best code example I have seen in a very long time...
Thank you!!!!
Also works with the binary formatters.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Good stuff - I am glad it is still of use... it could probably be modified now to selectively serialize the TreeNode directly using the DataMemberAttribute - .Net 3.0
Cheers
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Great Job Tom! I found out the hard way I couldn't pass TreeNodes via .NET Remoting... this let me get around that limitation. A nicy tidy little implementation - worked first time! And hey! You also showed me a new VB.NET operator this old VB6 programmer hadn't learned yet - "AndAlso"!! That's going to come in handy!!
Thanks again!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I spent about three hours trying to convert some c# code to vb.net to get this to work, and was going backwards rather than forwards.
As a last resort I went back to google and found this. In 20 minutes (ie virtually first time, once I resolved the bugs I had added myself) it was working.
I opened the downloaded code from VS2005 , and it did all the conversions for me.
Just great. Thanks very much
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
This code worked great until I tried to use a non-trivial Treeview node tag.
I need to extend the Treenode view with custom data. If I use the following, for example, the XML serializer chokes.
... TreeView1.SelectedNode.Tag = New Appconfig ... _ Public Class Appconfig Public Username as String Public Password as String End Class
The error is: System.InvalidOperationException was unhandled Message="There was an error generating the XML document." Source="System.Xml" StackTrace: at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o) at WindowsApplication1.TreeViewDataAccess.SaveTreeViewData(TreeView treeView, String path) in C:\temp\test\Form1.vb:line 459 ...
If I set the tag to a simple data type such as string, it works fine:
... TreeView1.SelectedNode.Tag = "string" ...
Any thoughts?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
FYI, the error is: "The type WindowsApplication1.Appconfig was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Add the Appconfig type to the extra types of the serializer object, it's a constructor overload. And make sure the class is marked as serializable.
Hope this helps
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Wow! It worked great. For anyone else interested, this is the updated code:
'Create a serializer and file to save TreeViewData Dim extraTypes(0) As Type extraTypes(0) = GetType(Appconfig) Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData), extraTypes)
Now I need to figure out how to get the tag to reload. Any ideas?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The ToTreeNode method should take care of that for you, there's a line in there that sets the treenode tag to your object:
ToTreeNode.Tag = Me.Tag
Boy... I really have to rewrite this article in 2.0... have not looked at the code in ages, half of it's redundant now!
Hope this helps
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I inserted some debug code to look at the tag.
When the tag is created by the application, it is type "WindowsApplication1.Appconfig". When I the app loads the XML, the tag is created as type "System.Xml.XmlNode[]".
If the tag is created as type "string", the tag gets loaded as type "string".
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
My mistake. I forgot to add the overload to ToTreeNode as well as SaveTreeViewData.
The code works great in VB 2005!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
i want to save in the xml format.ie just convert the treeview to xml form.here the elements are starting with etc i want an xml file including only the treeview contents can u help me
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
diees wrote: i want to save in the xml format.ie just convert the treeview to xml form.here the elements are starting with etc i want an xml file including only the treeview contents
This is exactly what the article discusses. Have you tried implementing it? Have a go, and if there are any particular elements you are struggling I will be pleased to help.
Cheers
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for taking the time to post this this sample. Even though it's only a begnning, sometimes all you need is a place to start. Rather than just use the tree node I've used a class that inherits the tree node. This way I can store anything I need against the node. Thanks again Simply Brilliant
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for the comments! Have to say, the article is ageing a bit now, really need to update it.
If there's something you think i should include, please let me know...
Cheers
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
As far as adding to this article, it would be great if the output of the serialization could be a string instead of a file.
I want to use this in Sharepoint to persist a treeview into a web part property so we don't have to do recursion to the database each time the page loads.
I'm going to do some research to see if I could stream this to a string.
Great article!!
Bonnie
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi Tom,
Man, your article saved my life... Thanks! 
Just a quick question: How do I save only the nodes that are selected? I know that the serialization saves the whole stuff but would like to "filter" the selected nodes.
Thanks again! 
-- modified at 15:42 Wednesday 21st February, 2007
-- modified at 15:43 Wednesday 21st February, 2007
"Happy is the man who finds wisdom, and the man who gains understanding." Proverbs 3:13
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Juba
When recusring through the node in the treeview to persist them:
'Populate the Nodes array with child nodes ReDim Nodes(treeview.Nodes.Count - 1) For i As Integer = 0 To treeview.Nodes.Count - 1 Nodes(i) = New TreeNodeData(treeview.Nodes(i)) Next
Change this so that you first cound the number of selected nodes and create the nodes array to that length and only add selected ones when iterating through the collection.
Hope this helps
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |