[mono-vb] Bug

Quan quandary82 at hailmail.net
Thu Nov 19 03:55:58 EST 2009


Every page should by default include the namespace System.Web.UI.WebControls
because when you declare RowDataBound in every version of Visual studio, 
you just write
Sub gvCurrentNavigation_RowDataBound(ByVal sender As Object, ByVal e As 
GridViewRowEventArgs)

Note that it is GridViewRowEventArgs
And not
System.Web.UI.WebControls.GridViewRowEventArgs
and System.Web.UI.WebControls is not included anywhere.
plus normally you can also just declare Dim x as Button, and not
Dim x as System.Web.UI.WebControls.Button


Also if you have a
Partial Class admin
Inherits System.Web.UI.Page
then this class includes the page namespace, and I shouldn't have to add 
System.Web.UI. in front of everytime I have the "Page" statement 
somewhere in my codebehind.

In the same chapter goes:
HttpContext
for mono it's System.Web.HttpContext
If you have an ASP.NET application in Visual Studio, this includes the 
namespace System.Web.
so you don't have to write System.Web. in front of it everywhere.


Microsoft.Jscript.GlobalObject.escape does not exist
It is a core function, since it's the only place where there is a 1:1 
equivalent of the javascript escape function. Without this function, you 
cannot JavaScript escape a string, which means you are unable to create 
a javascript messagebox which outputs vb.net exceptions.

I suggest incorporating the google v8 javascript engine.
Then you have complete support for any javascript function, you simply 
have to make an interface to it, which is not difficult, at least not 
for this one function.

Another issue:
Declaring this in the aspx:
<asp:Button ID="btnNewNodeOK" runat="server" Text="OK" />

Then, btnNewNodeOK can be perfectly handled with
Protected Sub btnNewNodeOK_Click(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnNewNodeOK.Click

note the handles clause.

It cannot be that I have to add every OnClick="btnNewNodeOK" and remove 
the handles statement from the codebehind for all my buttons or any 
other control's events, like linkbuttons.
If I have to do that, I can just as well rewrite the entire code in PHP, 
which would be better anyway, but I just don't have the time.


And then there is another thing:
My treeview serialzation gives a bug:
unimplemented compiler feature. Please file a bug report.

Here all the code in question
Class Debug is for the escape function
all others contain whatever leads to the unimplemented feature error


Imports Microsoft.VisualBasic
'Imports System
'Imports System.Data
'Imports System.Configuration
'Imports System.Web
'Imports System.Web.Security
'Imports System.Web.UI

Imports System.Web.Mail


Namespace MyName

Public Class Debug

Public Class MessageBox
Public Shared Sub Show(ByRef objMessage As Object, Optional ByRef 
strTitle As String = Nothing)
Dim strMessage As String = CStr(objMessage)
Dim pgCallingPage As Page = TryCast(HttpContext.Current.Handler, Page)
If pgCallingPage IsNot Nothing Then
strMessage = Microsoft.JScript.GlobalObject.escape(strMessage) ' 
Reference Microsoft.JScript.dll in the project reference
strMessage = "<script type=""text/javascript"" 
language=""javascript"">alert(unescape(""" + strMessage + """));</script>"
pgCallingPage.ClientScript.RegisterStartupScript(pgCallingPage.GetType(), 
System.Guid.NewGuid().ToString(), strMessage, False)
End If
End Sub
End Class


Public Shared Sub MsgBox(ByRef objMessage As Object, Optional ByRef 
strTitle As String = Nothing)
MessageBox.Show(objMessage, strTitle)
End Sub



Public Shared Sub WriteLine(ByRef objMessage As Object)
Dim strMessage As String = CStr(objMessage)
HttpContext.Current.Response.Write("<br/><p style=""font-family: 
Verdana, Calibri, Arial, Helvetica, Sans-Serif; font-color: #FF0000; 
font-size: 10mm;"">" + HttpContext.Current.Server.HtmlEncode(strMessage) 
+ "</p><br/>")
End Sub


Public Shared Sub SendMail()
Dim objSmtpMail As System.Net.Mail.SmtpClient = New 
System.Net.Mail.SmtpClient
Dim Mailmsg As New System.Net.Mail.MailMessage
Mailmsg.To.Clear()

'Mailmsg.To.Add(New System.Net.Mail.MailAddress("To Name 
<toname at todomain.com>")) ' Environment.UserName & "@knowdotnet.com"
Mailmsg.To.Add(New System.Net.Mail.MailAddress("somebody at somewhere.com", 
"Prename Name", System.Text.Encoding.UTF8))
'Mailmsg.To.Add(New System.Net.Mail.MailAddress("recipient2 at foo.bar.com"))
'Mailmsg.From = New System.Net.Mail.MailAddress("From Name 
<fromname at yourfromdomain.com>")
Mailmsg.From = New System.Net.Mail.MailAddress("somebody at somewhere.com", 
"Prename Name", System.Text.Encoding.UTF8)
'Mailmsg.CC.Add(New System.Net.Mail.MailAddress("CarbonCopy at foo.bar.com"))
'Mailmsg.Bcc.Add(New 
System.Net.Mail.MailAddress("BlindCarbonCopy at foo.bar.com"))

Mailmsg.Subject = "(subject text)"
'Mailmsg.Body = "(message text here)"
Mailmsg.Body = "<HTML><body><H1>Error: </H1><p>Error 
description</P></body></HTML>"
Mailmsg.IsBodyHtml = True 'set the content to HTML, and not plain text only


Mailmsg.BodyEncoding = System.Text.Encoding.UTF8
Mailmsg.SubjectEncoding = System.Text.Encoding.UTF8
Mailmsg.Priority = System.Net.Mail.MailPriority.Low

'Dim strFileNameAndPath As String = 
HttpContext.Current.Server.MapPath("myEmails") & "\Mypic.jpg"
'Dim objAttachment As Net.Mail.Attachment = New 
Net.Mail.Attachment(strFileNameAndPath)
'Mailmsg.Attachments.Add(objAttachment)


Try
objSmtpMail.Timeout = 15000
' Pass the credentials if the server requires the client to authenticate 
before it will send e-mail on the client's behalf.
objSmtpMail.Credentials = 
System.Net.CredentialCache.DefaultNetworkCredentials


Dim objSMTPuser As New System.Net.NetworkCredential()
objSMTPuser.UserName = "username"
objSMTPuser.Password = "password"
objSMTPuser.Domain = "smtp.tiscalinet.ch"


objSmtpMail.UseDefaultCredentials = False
'objSmtpMail.Credentials = objSMTPuser
objSmtpMail.Port = 25
objSmtpMail.Host = "smtp.tiscalinet.ch"
objSmtpMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network

'
objSmtpMail.Send(Mailmsg)
'objSmtpMail.Send(From, To, Subject, Body)
'objSmtpMail.Send(Environment.UserName & "@knowdotnet.com", 
"EmailAddress", "Subject", "Message")


' Async
'http://msdn.microsoft.com/de-de/library/system.net.mail.smtpclient.aspx
'http://msdn.microsoft.com/de-de/library/cc749732.aspx
'http://msdn.microsoft.com/de-de/library/cc749732.aspx#XSLTsection128121120120
'The userState can be any object that allows your callback
'method to identify this send operation.
'For this example, the userToken is a string constant.
'Dim strUserState As String = "test message1"
'AddHandler objSmtpMail.SendCompleted, AddressOf MailSendCompletedCallback
'objSmtpMail.SendAsync(Mailmsg, strUserState)

Catch ex As Exception
HttpContext.Current.Response.Write("Error: " & ex.ToString())
Finally
' Clean up.
Mailmsg.Dispose()
Mailmsg = Nothing
End Try



'System.Net.Mail reads SMTP configuration data out of the standard .NET 
configuration system
'(so for ASP.NET applications you’d configure this in your application’s 
web.config file).
'Here is an example of how to configure it:

'<system.net>
' <mailSettings>
' <smtp from="test at foo.com">
' <network host="smtpserver1" port="25" userName="username" 
password="secret" defaultCredentials="true" />
' </smtp>
' </mailSettings>
' </system.net>

End Sub






End Class

End Namespace


--- Start Treeview XML datasource ---
<?xml version="1.0" encoding="utf-16"?>
<node text="&lt;span id='c6f5ab9e-d08f-448a-9143-02d174317c07' 
oncontextmenu=&quot;SetContextMenu(this.id, event);return false;&quot; 
&gt;Altersheime der Stadt Zürich&lt;/span&gt;" 
value="c6f5ab9e-d08f-448a-9143-02d174317c07" 
navigateurl="javascript:SetUrl('./shownode.aspx?GUID=c6f5ab9e-d08f-448a-9143-02d174317c07');" 
populateondemand="False" showcheckbox="false" checked="False" 
expanded="True" selected="False">
<node text="&lt;span id='f28df8d9-f83f-494c-a88e-591c7266c455' 
oncontextmenu=&quot;SetContextMenu(this.id, event);return false;&quot; 
&gt;Altersheim Wildbach&lt;/span&gt;" 
value="f28df8d9-f83f-494c-a88e-591c7266c455" 
navigateurl="javascript:SetUrl('./shownode.aspx?GUID=f28df8d9-f83f-494c-a88e-591c7266c455');" 
populateondemand="False" showcheckbox="false" checked="False" 
expanded="True" selected="False">
<node text="&lt;span id='31360723-0be2-47e5-814c-4837c174c9a1' 
oncontextmenu=&quot;SetContextMenu(this.id, event);return false;&quot; 
&gt;Musterzimmer 203&lt;/span&gt;" 
value="31360723-0be2-47e5-814c-4837c174c9a1" 
navigateurl="javascript:SetUrl('./raumplaner.aspx?raum=31360723-0be2-47e5-814c-4837c174c9a1');" 
populateondemand="False" showcheckbox="false" checked="False" 
expanded="True" selected="False" />
<node text="&lt;span id='5accd32f-7a85-46de-99a3-0853843b4e99' 
oncontextmenu=&quot;SetContextMenu(this.id, event);return false;&quot; 
&gt;Musterzimmer 211&lt;/span&gt;" 
value="5accd32f-7a85-46de-99a3-0853843b4e99" 
navigateurl="javascript:SetUrl('./raumplaner.aspx?raum=5accd32f-7a85-46de-99a3-0853843b4e99');" 
populateondemand="False" showcheckbox="false" checked="False" 
expanded="True" selected="False" />
<node text="&lt;span id='67774365-233b-4176-93f4-65c4e4953daa' 
oncontextmenu=&quot;SetContextMenu(this.id, event);return false;&quot; 
&gt;Musterzimmer 511&lt;/span&gt;" 
value="67774365-233b-4176-93f4-65c4e4953daa" 
navigateurl="javascript:SetUrl('./raumplaner.aspx?raum=67774365-233b-4176-93f4-65c4e4953daa');" 
populateondemand="False" showcheckbox="false" checked="False" 
expanded="True" selected="False" />
</node>
</node>

--- End XML


Imports Microsoft.VisualBasic

Namespace MyName.XML
''' <summary>
''' Serializes and deserializes a TreeView
''' </summary>
Public Class TreeViewSerializer

' XML tag for node, e.g. 'node' in case of <node></node>
Private Const cstrXmlNodeTag As String = "node"
' XML attributes for node e.g. <node text="Asia" tag="" 
imageindex="1"></node>
Private Const cstrXmlNodeTextAtt As String = "text"
Private Const cstrXmlNodeToolTipAtt As String = "tooltip"
Private Const cstrXmlNodeTargetAtt As String = "target"
Private Const cstrXmlNodeValueAtt As String = "value"
Private Const cstrXmlNodeNavigateUrl As String = "navigateurl"
Private Const cstrXmlNodeImageUrlAtt As String = "imageurl"
Private Const cstrXmlNodeImageToolTipAtt As String = "imagetooltip"

Private Const cstrXmlNodePopulateOnDemandAtt As String = "populateondemand"
Private Const cstrXmlNodeShowCheckBoxAtt As String = "showcheckbox"
Private Const cstrXmlNodeCheckedAtt As String = "checked"
Private Const cstrXmlNodeExpandedAtt As String = "expanded"
Private Const cstrXmlNodeSelectedAtt As String = "selected"


Public Sub New()
' TODO: Add a constructor here
End Sub


Public Sub SaveTreeViewToXMLfile(ByRef tvTreeView As TreeView, ByRef 
strFileNameAndPath As String)
Dim xtrXMLtextWriter As New System.Xml.XmlTextWriter(strFileNameAndPath, 
System.Text.Encoding.Unicode)
xtrXMLtextWriter.Formatting = System.Xml.Formatting.Indented
xtrXMLtextWriter.WriteStartDocument() ' Writing the XML declaration
'xtrXMLtextWriter.WriteStartElement("TreeView") ' Writing the main 
element which encloses all node tags
' xtrXMLtextWriter.WriteRaw("\r\n");

' Save the nodes recursively
SaveNodes(tvTreeView.Nodes, xtrXMLtextWriter)

'xtrXMLtextWriter.WriteEndElement()
xtrXMLtextWriter.Close()
End Sub


Private Sub SaveNodes(ByRef tncTreeNodeCollection As TreeNodeCollection, 
ByRef xtwXMLtextWriter As System.Xml.XmlTextWriter)
For iNodeIndex As Integer = 0 To tncTreeNodeCollection.Count - 1
Dim tnTreeNode As TreeNode = tncTreeNodeCollection(iNodeIndex)
xtwXMLtextWriter.WriteStartElement(cstrXmlNodeTag)


''''''''''''''''''''''''''''''''''''''''''''''''''''
' Write TreeNode Attributes to XML file
''''''''''''''''''''''''''''''''''''''''''''''''''''
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeTextAtt, tnTreeNode.Text)

If Not String.IsNullOrEmpty(tnTreeNode.ToolTip) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeToolTipAtt, 
tnTreeNode.ToolTip.ToString())
End If


If Not String.IsNullOrEmpty(tnTreeNode.Target) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeTargetAtt, 
tnTreeNode.Target.ToString())
End If

If Not String.IsNullOrEmpty(tnTreeNode.Value) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeValueAtt, 
tnTreeNode.Value.ToString())
End If

If String.IsNullOrEmpty(tnTreeNode.NavigateUrl) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeNavigateUrl, "")
Else
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeNavigateUrl, 
tnTreeNode.NavigateUrl.ToString())
End If

If Not String.IsNullOrEmpty(tnTreeNode.ImageUrl) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeImageUrlAtt, 
tnTreeNode.ImageUrl.ToString())
End If

If Not String.IsNullOrEmpty(tnTreeNode.ImageToolTip) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeImageToolTipAtt, 
tnTreeNode.ImageToolTip.ToString())
End If

If Not String.IsNullOrEmpty(tnTreeNode.PopulateOnDemand) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodePopulateOnDemandAtt, 
tnTreeNode.PopulateOnDemand.ToString())
End If


'If tnTreeNode.ShowCheckBox IsNot Nothing Then
'xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeShowCheckBoxAtt, 
tnTreeNode.ShowCheckBox.ToString())
'End If

If IsNothing(tnTreeNode.ShowCheckBox) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeShowCheckBoxAtt, "false")
Else
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeShowCheckBoxAtt, 
tnTreeNode.ShowCheckBox.ToString())
End If

If Not String.IsNullOrEmpty(tnTreeNode.Checked) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeCheckedAtt, 
tnTreeNode.Checked.ToString())
End If


'If tnTreeNode.Expanded IsNot Nothing Then
'xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeExpandedAtt, 
tnTreeNode.Expanded.ToString())
'End If

If IsNothing(tnTreeNode.Expanded) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeExpandedAtt, "false")
Else

xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeExpandedAtt, 
tnTreeNode.Expanded.ToString())
End If






If Not String.IsNullOrEmpty(tnTreeNode.Selected) Then
xtwXMLtextWriter.WriteAttributeString(cstrXmlNodeSelectedAtt, 
tnTreeNode.Selected.ToString())
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''



''''''''''''''''''''''''''''''''''''''''''''''''''''
' Todo:
' Add other node properties for serialization here
''''''''''''''''''''''''''''''''''''''''''''''''''''

If tnTreeNode.ChildNodes.Count > 0 Then
SaveNodes(tnTreeNode.ChildNodes, xtwXMLtextWriter)
End If
xtwXMLtextWriter.WriteEndElement()
Next
End Sub


Public Sub LoadXMLfileIntoTreeView(ByRef tvTreeView As TreeView, ByRef 
strFileNameAndPath As String)
Dim xtrXMLtextReader As System.Xml.XmlTextReader = Nothing
Try
' Disabling re-drawing of treeview 'till all nodes are added
' TreeView.BeginUpdate()
xtrXMLtextReader = New System.Xml.XmlTextReader(strFileNameAndPath)

Dim tnParentNode As TreeNode = Nothing
While xtrXMLtextReader.Read()
Select Case xtrXMLtextReader.NodeType
Case System.Xml.XmlNodeType.XmlDeclaration, 
System.Xml.XmlNodeType.Whitespace
Continue While ' Ignore, that's faster
Case System.Xml.XmlNodeType.Element
If xtrXMLtextReader.Name = cstrXmlNodeTag Then
Dim tnNewTreeNode As New TreeNode()
Dim bisEmptyElement As Boolean = xtrXMLtextReader.IsEmptyElement

' Loading attributes for node
Dim iAttributeCount As Integer = xtrXMLtextReader.AttributeCount
If iAttributeCount > 0 Then
For iAttributeCounter As Integer = 0 To iAttributeCount - 1
xtrXMLtextReader.MoveToAttribute(iAttributeCounter)

SetAttributeValue(tnNewTreeNode, xtrXMLtextReader.Name, 
xtrXMLtextReader.Value)
Next
End If

' Add a new node to Parent Node or TreeView
If tnParentNode IsNot Nothing Then
tnParentNode.ChildNodes.Add(tnNewTreeNode)
Else
tvTreeView.Nodes.Add(tnNewTreeNode)
End If

' Making the current node the 'ParentNode' if it's not empty
If Not bisEmptyElement Then
tnParentNode = tnNewTreeNode
End If
End If
' Moving up to in TreeView if end tag is encountered
Case System.Xml.XmlNodeType.EndElement
If xtrXMLtextReader.Name = cstrXmlNodeTag Then
tnParentNode = tnParentNode.Parent
End If
Case System.Xml.XmlNodeType.None
'Exit Sub
Continue While
Case System.Xml.XmlNodeType.Text
Dim tnNewTreeNode As System.Web.UI.WebControls.TreeNode = New 
System.Web.UI.WebControls.TreeNode
tnNewTreeNode.Text = xtrXMLtextReader.Value
tnParentNode.ChildNodes.Add(tnNewTreeNode)
End Select

End While

Catch ex As Exception
HttpContext.Current.Response.Write("Exception in 
LoadXMLfileIntoTreeView.<br />Description:<br />" + ex.Message)
Finally
' Enabling redrawing of treeview after all nodes are added
' TreeView.EndUpdate()
xtrXMLtextReader.Close()
End Try
End Sub



''' <summary>
''' Used by the LoadXmlFileIntoTreeView method for setting properties of 
TreeNode from the attributes of the XML node
''' </summary>
''' <param name="tnThisNode"></param>
''' <param name="strPropertyName"></param>
''' <param name="strValue"></param>
Private Sub SetAttributeValue(ByRef tnThisNode As TreeNode, ByRef 
strPropertyName As String, ByRef strValue As String)
Select Case strPropertyName
Case cstrXmlNodeTextAtt
tnThisNode.Text = strValue
Case cstrXmlNodeImageUrlAtt
tnThisNode.ImageUrl = strValue
Case cstrXmlNodeToolTipAtt
tnThisNode.ToolTip = strValue
Case cstrXmlNodeTargetAtt
tnThisNode.Target = strValue
Case cstrXmlNodeValueAtt
tnThisNode.Value = strValue
Case cstrXmlNodeNavigateUrl
tnThisNode.NavigateUrl = strValue
Case cstrXmlNodeImageUrlAtt
tnThisNode.ImageUrl = strValue
Case cstrXmlNodeImageToolTipAtt
tnThisNode.ImageToolTip = strValue
Case cstrXmlNodePopulateOnDemandAtt
tnThisNode.PopulateOnDemand = strValue
Case cstrXmlNodeShowCheckBoxAtt
tnThisNode.ShowCheckBox = strValue
Case cstrXmlNodeCheckedAtt
tnThisNode.Checked = strValue
Case cstrXmlNodeExpandedAtt
tnThisNode.Expanded = strValue
Case cstrXmlNodeSelectedAtt
tnThisNode.Selected = strValue
End Select
End Sub


Public Sub LoadXmlFileInTreeView(ByVal tvTreeView As TreeView, ByVal 
strFileNameAndPath As String)
Dim xtrXMLtextReader As System.Xml.XmlTextReader = Nothing
Try
' tvTreeView.BeginUpdate()
xtrXMLtextReader = New System.Xml.XmlTextReader(strFileNameAndPath)

Dim tnCurrentSelectedTreeNode As New TreeNode(strFileNameAndPath)
tvTreeView.Nodes.Add(tnCurrentSelectedTreeNode)
While xtrXMLtextReader.Read()


Select Case xtrXMLtextReader.NodeType
Case System.Xml.XmlNodeType.XmlDeclaration, 
System.Xml.XmlNodeType.Whitespace
Continue While ' Ignore, that's faster
Case System.Xml.XmlNodeType.Element
Dim bisEmptyElement As Boolean = xtrXMLtextReader.IsEmptyElement
Dim sbCurrentItemString As New System.Text.StringBuilder()
sbCurrentItemString.Append(xtrXMLtextReader.Name)
Dim iAttributeCount As Integer = xtrXMLtextReader.AttributeCount
If iAttributeCount > 0 Then
sbCurrentItemString.Append(" ( ")
For iAttributeCounter As Integer = 0 To iAttributeCount - 1
If iAttributeCounter <> 0 Then
sbCurrentItemString.Append(", ")
End If
xtrXMLtextReader.MoveToAttribute(iAttributeCounter)
sbCurrentItemString.Append(xtrXMLtextReader.Name)
sbCurrentItemString.Append(" = ")
sbCurrentItemString.Append(xtrXMLtextReader.Value)
Next
sbCurrentItemString.Append(" ) ")
End If

If bisEmptyElement Then
Dim tnNewTreeNode1 As System.Web.UI.WebControls.TreeNode = New 
System.Web.UI.WebControls.TreeNode
tnNewTreeNode1.Text = sbCurrentItemString.ToString()
tnCurrentSelectedTreeNode.ChildNodes.Add(tnNewTreeNode1)
Else
Dim tnNewTreeNode2 As System.Web.UI.WebControls.TreeNode = New 
System.Web.UI.WebControls.TreeNode
tnNewTreeNode2.Text = sbCurrentItemString.ToString()
tnCurrentSelectedTreeNode.ChildNodes.Add(tnNewTreeNode2)
tnCurrentSelectedTreeNode = tnNewTreeNode2
End If
Case System.Xml.XmlNodeType.EndElement
If Not tnCurrentSelectedTreeNode.Depth = 0 Then ' And if it is not the 
root node...
tnCurrentSelectedTreeNode = tnCurrentSelectedTreeNode.Parent
End If
Case System.Xml.XmlNodeType.None
Continue While ' Exit Sub
Case System.Xml.XmlNodeType.Text
Dim tnNewTreeNode3 As System.Web.UI.WebControls.TreeNode = New 
System.Web.UI.WebControls.TreeNode
tnNewTreeNode3.Text = xtrXMLtextReader.Value
tnCurrentSelectedTreeNode.ChildNodes.Add(tnNewTreeNode3)
End Select

End While
Catch ex As Exception
HttpContext.Current.Response.Write("Exception in 
LoadXmlFileInTreeView.<br>Description:<br>" + ex.Message)
Finally
' tvTreeView.EndUpdate()
xtrXMLtextReader.Close()
End Try
End Sub


End Class


End Namespace




Namespace MyName.XMLserialization

Public Class XmlTextWriterIndentedStandaloneNo
Inherits System.Xml.XmlTextWriter


Public Sub New(ByRef w As System.IO.TextWriter)
MyBase.New(w)
Formatting = System.Xml.Formatting.Indented
End Sub


Public Sub New(ByRef strFileName As String, ByRef teEncoding As 
System.Text.Encoding)
MyBase.New(strFileName, teEncoding)
Formatting = System.Xml.Formatting.Indented
End Sub


Public Sub New(ByRef w As System.IO.Stream, ByRef teEncoding As 
System.Text.Encoding)
MyBase.New(w, teEncoding)
Formatting = System.Xml.Formatting.Indented
End Sub


Public Overloads Overrides Sub WriteStartDocument()
' Or suppress by just ommitting WriteStartDocument
MyBase.WriteStartDocument(False) ' False: Standalon="no"
End Sub
End Class


Public Class XMLserializeFacility


Public Shared Sub SerializeToXML(ByRef strFileNameAndPath As String, 
ByVal ThisFacility As cFacility)
Dim serializer As New 
System.Xml.Serialization.XmlSerializer(GetType(cFacility))

Dim xtwXMLtextWriter As System.Xml.XmlTextWriter = Nothing
Try

xtwXMLtextWriter = New System.Xml.XmlTextWriter(strFileNameAndPath, 
System.Text.Encoding.UTF8)
'xtwXMLtextWriter = New 
XmlTextWriterIndentedStandaloneNo("C:\Users\username\Desktop\furniture.xml", 
System.Text.Encoding.UTF8)

xtwXMLtextWriter.Formatting = System.Xml.Formatting.Indented

Dim ns As New System.Xml.Serialization.XmlSerializerNamespaces()
' Get rid of
' xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
' and
' xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
ns.Add("", "")

serializer.Serialize(xtwXMLtextWriter, ThisFacility, ns)
'serializer.Serialize(xtwXMLtextWriter, MyAppConfig)

xtwXMLtextWriter.Flush()
xtwXMLtextWriter.Close() 'Write the XML to file and close the writer
Catch ex As Exception
Console.WriteLine("Encountered Exception in 
MyName.XMLserialization.SerializeToXML()" + vbCrLf + "Details: " + 
vbCrLf + ex.Message)
End Try


'Dim swEncodingWriter As System.IO.StreamWriter = New 
System.IO.StreamWriter("C:\Users\username\Desktop\furniture.xml", False, 
System.Text.Encoding.UTF8)
'serializer.Serialize(swEncodingWriter, MyAppConfig)
'swEncodingWriter.Close()
'swEncodingWriter.Dispose()
End Sub


Public Shared Sub SerializeToXML(ByRef strFileNameAndPath As String, 
ByVal FacilityList As System.Collections.Generic.List(Of cFacility))
Dim serializer As New 
System.Xml.Serialization.XmlSerializer(GetType(System.Collections.Generic.List(Of 
cFacility)))
Dim swEncodingWriter As System.IO.StreamWriter = New 
System.IO.StreamWriter(strFileNameAndPath, False, System.Text.Encoding.UTF8)
serializer.Serialize(swEncodingWriter, FacilityList)
swEncodingWriter.Close()
swEncodingWriter.Dispose()
End Sub


' "C:\Users\username\Desktop\furniture.xml"
Public Shared Function DeserializeFromXML(ByRef strFileNameAndPath As 
String) As MyName.XMLserialization.cFacility
Dim deserializer As New 
System.Xml.Serialization.XmlSerializer(GetType(cFacility))
Dim srEncodingReader As IO.StreamReader = New 
IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
'Dim textReader As IO.TextReader = New 
IO.StreamReader("C:\Users\username\Desktop\furniture.xml")
Dim ThisFacility As cFacility

ThisFacility = DirectCast(deserializer.Deserialize(srEncodingReader), 
cFacility)
srEncodingReader.Close()
srEncodingReader.Dispose()

Return ThisFacility
End Function


Public Shared Function DeserializeFromXML1(ByRef strFileNameAndPath As 
String) As System.Collections.Generic.List(Of 
MyName.XMLserialization.cFacility)
Dim deserializer As New 
System.Xml.Serialization.XmlSerializer(GetType(System.Collections.Generic.List(Of 
cFacility)))
Dim srEncodingReader As IO.StreamReader = New 
IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
'Dim textReader As IO.TextReader = New 
IO.StreamReader("C:\Users\username\Desktop\furniture.xml")
Dim FacilityList As System.Collections.Generic.List(Of cFacility)
FacilityList = DirectCast(deserializer.Deserialize(srEncodingReader), 
System.Collections.Generic.List(Of cFacility))
srEncodingReader.Close()
srEncodingReader.Dispose()

Return FacilityList
End Function

End Class


' Actually there is a more appropriate newsgroup:
' microsoft.public.dotnet.xml
'http://bytes.com/topic/net/answers/102955-xml-serialization-class-attributes
Public Class cPoints
' a named attribute
<System.Xml.Serialization.XmlAttribute("x")> _
Public X As Double = 0.0

<System.Xml.Serialization.XmlAttribute("y")> _
Public y As Double = 0.0
End Class


Public Class cArea
'<area name="1" color="0x999999" alpha="20" strokecolor="0xffffff" 
strokealpha="00" showstroke="false">
<System.Xml.Serialization.XmlAttribute("name")> _
Public name As String = "1"

<System.Xml.Serialization.XmlAttribute("color")> _
Public color As String = "0x999999"

<System.Xml.Serialization.XmlAttribute("alpha")> _
Public alpha As Integer = 0

<System.Xml.Serialization.XmlAttribute("strokecolor")> _
Public strokecolor As String = "0xffffff"

<System.Xml.Serialization.XmlAttribute("strokealpha")> _
Public strokealpha As String = "00"

<System.Xml.Serialization.XmlAttribute("showstroke")> _
Public showstroke As Boolean = False

<System.Xml.Serialization.XmlElement("point")> _
Public Area As New System.Collections.Generic.List(Of cPoints)
End Class


Public Class cAreas
<System.Xml.Serialization.XmlElement("area")> _
Public AreaList As New System.Collections.Generic.List(Of cArea)
End Class


Public Class cStock
' <stock id="0" name="" src="images/moebel_2/bett_200_90.swf" 
width="0.90" height="2.00" />
<System.Xml.Serialization.XmlAttribute("id")> _
Public id As Integer = 0

<System.Xml.Serialization.XmlAttribute("name")> _
Public name As String = ""

<System.Xml.Serialization.XmlAttribute("src")> _
Public src As String = "DefaultSource"

<System.Xml.Serialization.XmlAttribute("width")> _
Public width As Double = 0.0

<System.Xml.Serialization.XmlAttribute("height")> _
Public height As Double = 0.0
End Class


Public Class cStocks
'<stocks type ="Betten/Nachttische">
<System.Xml.Serialization.XmlAttribute("type")> _
Public type As String = "DefaultFurniture"


<System.Xml.Serialization.XmlElement("stock")> _
Public StockList As New System.Collections.Generic.List(Of cStock)
End Class


Public Class cStocklib
'<stocklib name="M�bel-Bibliothek" icon="images/icons/moebel_icon.jpg">
<System.Xml.Serialization.XmlAttribute("name")> _
Public name As String = "DefaultName"

<System.Xml.Serialization.XmlAttribute("icon")> _
Public icon As String = "DefaultIcon"


<System.Xml.Serialization.XmlElement("stocks")> _
Public Stock As New System.Collections.Generic.List(Of cStocks)
End Class


Public Class cFurniture
'<furniture src ="" posX="" posY="" />
<System.Xml.Serialization.XmlAttribute("src")> _
Public strSource As String

<System.Xml.Serialization.XmlAttribute("posX")> _
Public dPosX As Double

<System.Xml.Serialization.XmlAttribute("posY")> _
Public dPosY As Double

'<System.Xml.Serialization.XmlIgnore()> _
Public Overrides Function ToString() As String
Return "Src: " + strSource + " PosX: " + dPosX.ToString + "PosY: " + 
dPosY.ToString
End Function
End Class


Public Class cFurnitureList
<System.Xml.Serialization.XmlElement("furniture")> _
Public Stock As New System.Collections.Generic.List(Of cFurniture)
End Class



Public Class cScale
' a named attribute
<System.Xml.Serialization.XmlAttribute("define")> _
Public dblScale As Double = 0.0
End Class


<System.Xml.Serialization.XmlRoot("facility")> _
Public Class cFacility

<System.Xml.Serialization.XmlIgnore()> _
Public Property Scale() As Double
Get
Return Me._Scale.dblScale
End Get
Set(ByVal value As Double)
Me._Scale.dblScale = value
End Set
End Property



<System.Xml.Serialization.XmlElement("scale")> _
Public _Scale As cScale = New cScale

<System.Xml.Serialization.XmlElement("areas")> _
Public areas As cAreas = New cAreas

<System.Xml.Serialization.XmlElement("stocklib")> _
Public Stocklib As New System.Collections.Generic.List(Of cStocklib)

' the text attribute
'<System.Xml.Serialization.XmlTextAttribute()> _
'Public Type As String = "MeinTyp"
'Public Status As Boolean = False
End Class


End Namespace



More information about the Mono-vb mailing list