We help IT Professionals succeed at work.

Problem displaying xml in textbox

e_murf1
e_murf1 asked
on
923 Views
Last Modified: 2008-04-24
Hi,
  I need to load an xml, edit it and display it in a textbox but when I do that the entire xml is on the first line of the textbox. The only way I can get the xml to look ok in the textbox is to load the xml, edit it, save it to a temp file & use a stream reader to read it back in as a string.

Does anyone know how I can do this without writing to a temp file?
Thanks.
Comment
Watch Question

Top Expert 2007

Commented:
Do you have your textbox MultiLine property set to True or False?  Should be set to True.
CERTIFIED EXPERT

Commented:
1.- make your textbox multiline and check accept enter (properties)
2.-then try
    dim tmp as string=replace(yourXML,chr(13) & chr(10),vbnewline)
    tmp =replace(tmp,chr(10),vbnewline)
    me.textbox1.text=tmp
    tmp=nothing
Hint
Do not use tmp.replace function -you might get an error, replace(tmp,.... will finish with no errors

Author

Commented:
Hi,
The textbox is set to multiline but its a rich textbox so there is no accepts enter property. I changed yourXML to yourXML.outerxml because it wouldn't accept it but it did not work. I did a search for chr(13) & chr(10) on the xml.outerxml & there was none. When I use the xml.save at the moment it must format it somehow to add the carriage returns. Then I just read it in line by line. I've know idea how the .save formats it though.
CERTIFIED EXPERT

Commented:
Is your xml file located on your disk or you read it from the internet? and what is the code you read it?

Author

Commented:
Hi,
Sorry its taken so long for me to reply.
The xml is on my disk. I put together an example to show you my problem. I don't want to have to write the file back out after I edit the xml but I can't see any other way to have the xml formated properly in the textbox.
Thanks,
Eoghan.

Imports System.IO
Imports System.Xml

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents RTB As System.Windows.Forms.RichTextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.RTB = New System.Windows.Forms.RichTextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'RTB
        '
        Me.RTB.Location = New System.Drawing.Point(200, 48)
        Me.RTB.Name = "RTB"
        Me.RTB.Size = New System.Drawing.Size(312, 296)
        Me.RTB.TabIndex = 0
        Me.RTB.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(552, 56)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(56, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(552, 96)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(56, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(904, 454)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RTB)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xd As New XmlDocument
        Dim XmlPath As String = Application.StartupPath & "\Test.xml"
        xd.Load(XmlPath)
        xd.SelectSingleNode("//Value").InnerText = 2
        RTB.Text = xd.OuterXml
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim xd As New XmlDocument
        Dim XmlPath As String = Application.StartupPath & "\Test.xml"
        xd.Load(XmlPath)
        xd.SelectSingleNode("//Value").InnerText = 2
        xd.Save(XmlPath)

        Dim sr As New StreamReader(XmlPath)
        Dim linein As String = ""
        linein = sr.ReadToEnd
        sr.Close()

        RTB.Text = linein

    End Sub
End Class

Author

Commented:
You'll need to add an xml into the startuppath that looks like this

<Test>
  <Value>5</Value>
</Test>
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.