XML Creation

Hi,

Hopefully this is the correct place for this type of (probably quite basic) question..

apologies if it is not..

I have some html (that is xhtml compliant) and I want to add it to a xmldocument that represents a

part of a web page (I know which node in the document I want to append the text to)..

However I am not sure of the best way to do this..

I began by creating a xmlelement and setting the inner text to the html fragment, however when doing this I found that the html is automatically escaped (i.e. "<" becomes "&lt;" etc) I don’t know how long the incoming html will be and thought there must be a simpler way than writing a parser to parse the incoming string and add each node (from the html) one at a time, however I have as yet been able to figure this out...

Thank you for any help...

km

[3453 byte] By [Kenny99] at [2007-12-28]
# 1

Hi,

This works for Visual Studio 2003.

Maybe someone can translate the foreign variable names to English, anyone please?

I have not got the time right now.

It is a project i got working for another user that had a very small

error in it. :-)

_

Anyway.......

Go to the PROJECT MENU and add this CLASS as clsErro.Vb

Sorry the variable words are foreign but this is not my solution.

I just know it works and writes to a file named "C:\erro.XML"

Next add the FORM code which is in the next post to a completely empty code window.

Imports System.io

Imports System.Xml.Serialization

<XmlRoot("Erros"), XmlType("Erros")> _

Public Class clsErro

Private m_Info As String

Private m_datData As Date

Private m_strDescricao As String

Private m_strNumeroErro As String

Private m_strCodigoUtilizador As String

Public strLocalizacaoBds As String = "C:\" 'This is a part path string i guess?

Public m_strLocalizacaoFicheiroErro As String = strLocalizacaoBds & "erro.XML"

Public Sub New()

m_datData = Now.Date

m_Info = ""

m_strDescricao = ""

m_strNumeroErro = ""

m_strCodigoUtilizador = ""

End Sub

Public Sub New(ByVal strInfo As String, ByVal datdata As Date, ByVal strDescricao As String, _

ByVal strNumeroErro As String, ByVal strCodUtilizador As String)

m_datData = datdata

m_Info = strInfo

m_strDescricao = strDescricao

m_strNumeroErro = strNumeroErro

m_strCodigoUtilizador = strCodUtilizador

End Sub

Public ReadOnly Property ficheiroErro()

Get

Return m_strLocalizacaoFicheiroErro

End Get

End Property

Public Property data() As Date

Get

Return m_datData

End Get

Set(ByVal value As Date)

m_datData = value

End Set

End Property

Public Property descricao() As String

Get

Return m_strDescricao

End Get

Set(ByVal value As String)

m_strDescricao = value

End Set

End Property

Public Property numeroErro() As String

Get

Return m_strNumeroErro

End Get

Set(ByVal value As String)

m_strNumeroErro = value

End Set

End Property

Public Property utilizador() As String

Get

Return m_strCodigoUtilizador

End Get

Set(ByVal value As String)

m_strCodigoUtilizador = value

End Set

End Property

Public Property info() As String

Get

Return m_Info

End Get

Set(ByVal value As String)

m_Info = value

End Set

End Property

End Class

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hi,

Here is the FORM code. It writes the XML file when button1 is clicked.>>

Imports System.io

Imports System.Xml.Serialization

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 Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(72, 160)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Button1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Public Sub writeErrorData(ByVal strInfo As String, ByVal strDescricao As String, _

ByVal strNumeroErro As String)

Dim strCodUtilizador As String = ""

Dim erro As New clsErro(strInfo, strData, strDescricao, strNumeroErro, strCodUtilizador)

Dim erroPath As String = erro.ficheiroErro.ToString

Dim xRoot As New XmlRootAttribute("Erro")

xRoot.IsNullable = True

Dim writer As New XmlSerializer(GetType(clsErro), xRoot)

Dim file As New StreamWriter(erroPath, True)

writer.Serialize(file, erro)

file.Close()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

writeErrorData("TestString", "Testing", "7")

End Sub

End Class

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Hi,

sorry.. I should be a little more clear in the description..

I dont want to write a file out;

I have a xmlDocument object, that is populated with a number of nodes.

The document represents an asp page (which is xml compliant)

What I want to do is add some more nodes to the xmlDocument object

(such as :

<tr>

<th class="aaa" style="width: 100px">aa1</th>

<th class="bbb" style="width: 1200px">bb1</th>

<td class="ccc" style="width: 250px">cc1</td>

</tr>)

However I will be receiving these nodes as a string (of arbitary length).

I I try and add this string to an existing node in the xmlDocument object as the inner text

(i.e. formDoc.FirstChild.InnerText = sHtml

- where formDoc is a xmlDocument and sHtml is a string containing the above html fragment)

of the node, then all the symbols are escaped (i.e. "<" becomes "&lt;" etc..)

Which means that when the xmlDocument is written to file and rendered in a browser,

it will not display properly..

Therefor I need to work out how to take the html fragment and somehow convert ti to a series of xml nodes

(hopefully without writing a parser etc to do it on a node by node basis)

i.e. something like xmlDocument.Load("some xml as a string)

but for a node/element would be useful...

Any further help would be much appreciated,

Thanks

km

Kenny99 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Hi Kenny

Is this the sort of thing you are looking for?

Dim xml As New Xml.XmlDocument : xml.Load("filename")

Dim node As Xml.XmlNode = xml.SelectSingleNode("xpath")

node.InnerXml &= "xml"

The obvious drawback here is that the xml string you are adding must be well formed.

Richard

DickDonny at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

Hi,

Thanks yeah - I didnt realise it could be done that way -

I've now got it going as:

Dim dynamicFragment As XmlDocumentFragment = parentDocument.CreateDocumentFragment()

dynamicFragment.InnerXml = dynamicHtml

placeholderStartNode.AppendChild(dynamicFragment)

Again the xml needs to be well formed - but that is fine as it should be in all cases..

Not sure which is best?

Thanks for the help,

Kenny

Kenny99 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

Hi,

Thanks yeah - I didnt realise it could be done that way -

I've now got it going as:

Dim dynamicFragment As XmlDocumentFragment = parentDocument.CreateDocumentFragment()

dynamicFragment.InnerXml = dynamicHtml

placeholderStartNode.AppendChild(dynamicFragment)

Again the xml needs to be well formed - but that is fine as it should be in all cases..

Not sure which is best?

Thanks for the help,

Kenny

Kenny99 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...