0
所以我建立一個小的Windows窗體應用程序,它可以讓你選擇一個消息斷的MSMQ隊列,然後一個對話框形式顯示在一個很好的格式化觀衆的XML消息。對話框窗體使用Rich Text Box控件。的XMLReader IsElementEmpty是錯誤的
一旦消息的主體被讀入一個如下。我創造了這個擴展方法來添加換行符和格式:
<Extension()>
Public Function Beautify(doc As XmlDocument) As String
Dim sb As New StringBuilder()
Dim settings As New XmlWriterSettings()
With settings
.Indent = True
.IndentChars = " "
.NewLineChars = Environment.NewLine
.NewLineHandling = NewLineHandling.Replace
.Encoding = New UTF8Encoding(False)
End With
Using writer As XmlWriter = XmlWriter.Create(sb, settings)
If TypeOf doc.ChildNodes(0) Is XmlProcessingInstruction Then
doc.RemoveChild(doc.ChildNodes(0))
End If
doc.Save(writer)
Return sb.ToString()
End Using
End Function
我創造,使多個消息可以選擇一個目錄(串),然後我通過這個進我的對話形式的自定義構造函數:
Public Sub New(lMessages As List(Of String))
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim strLastMessage As String = lMessages.Last()
For Each strMessage As String In lMessages
Using reader As XmlReader = XmlReader.Create(New StringReader(strMessage))
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.XmlDeclaration
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText("<? " & reader.Name & " " & reader.Value & " ?>")
Case XmlNodeType.Element
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText("<")
Me.rtbMessage.SelectionColor = Color.Brown
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(reader.Name)
If reader.HasAttributes Then
While reader.MoveToNextAttribute()
Me.rtbMessage.SelectionColor = Color.Brown
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(" " & reader.Name)
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText("=" & Chr(34))
Me.rtbMessage.SelectionColor = Color.Black
Me.rtbMessage.SelectionFont = New Font(Me.rtbMessage.Font, FontStyle.Bold)
Me.rtbMessage.AppendText(reader.Value)
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(Chr(34))
End While
End If
If reader.IsEmptyElement = True Then
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(" />")
Else
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(">")
End If
Case XmlNodeType.Text
Me.rtbMessage.SelectionColor = Color.Black
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(reader.Value)
Case XmlNodeType.EndElement
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText("</")
Me.rtbMessage.SelectionColor = Color.Brown
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(reader.Name)
Me.rtbMessage.SelectionColor = Color.Blue
Me.rtbMessage.SelectionFont = Me.rtbMessage.Font
Me.rtbMessage.AppendText(">")
Case XmlNodeType.Whitespace
Me.rtbMessage.AppendText(reader.Value)
End Select
End While
End Using
If strMessage <> strLastMessage Then
Me.rtbMessage.AppendText(Chr(13) & Chr(10))
Me.rtbMessage.AppendText(Chr(13) & Chr(10))
End If
Next
Me.rtbMessage.Select(0, 0)
End Sub
現在大部分工作很好,除了自閉元素沒有收到關閉「/>」。我想我的If-Then語句,我檢查reader.IsEmptyElement會做的伎倆,但事實並非如此。
下面是一個示例XML文件:
<Animal type="Mammal">
<Classifications>
<Classify name="CommonName" value="Dog" />
<Classify name="Environment" value="Land" />
<Classify name="NumLegs" value="4" />
</Classifications>
</Animal>
如果你運行我的代碼,你會看到「分類」元素沒有標籤收盤呈現。
任何幫助將不勝感激!如果你更瞭解C#,那麼用該語言的解決方案也不錯。我可以轉換!