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 "<" 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]
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.ioImports
System.Xml.Serialization<XmlRoot("Erros"), XmlType("Erros")> _
Public
Class clsErroPrivate m_Info As StringPrivate m_datData As DatePrivate m_strDescricao As StringPrivate m_strNumeroErro As StringPrivate m_strCodigoUtilizador As StringPublic 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 SubPublic 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 SubPublic ReadOnly Property ficheiroErro()GetReturn m_strLocalizacaoFicheiroErroEnd GetEnd Property
Public Property data() As DateGetReturn m_datDataEnd GetSet(ByVal value As Date)m_datData = value
End SetEnd PropertyPublic Property descricao() As StringGetReturn m_strDescricaoEnd GetSet(ByVal value As String)m_strDescricao = value
End SetEnd PropertyPublic Property numeroErro() As StringGetReturn m_strNumeroErroEnd GetSet(ByVal value As String)m_strNumeroErro = value
End SetEnd PropertyPublic Property utilizador() As StringGetReturn m_strCodigoUtilizadorEnd GetSet(ByVal value As String)m_strCodigoUtilizador = value
End SetEnd PropertyPublic Property info() As StringGetReturn m_InfoEnd GetSet(ByVal value As String)m_Info = value
End SetEnd PropertyEnd
Class
Hi,
Here is the FORM code. It writes the XML file when button1 is clicked.>>
Imports
System.io
Imports
System.Xml.SerializationPublic
Class Form1Inherits 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() callEnd Sub'Form overrides dispose to clean up the component list.Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)If disposing ThenIf Not (components Is Nothing) Thencomponents.Dispose()
End IfEnd IfMyBase.Dispose(disposing)End Sub'Required by the Windows Form DesignerPrivate 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.ButtonMe.SuspendLayout()''Button1'Me.Button1.Location = New System.Drawing.Point(72, 160)Me.Button1.Name = "Button1"Me.Button1.TabIndex = 0Me.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 RegionPublic 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.ToStringDim xRoot As New XmlRootAttribute("Erro")xRoot.IsNullable =
TrueDim writer As New XmlSerializer(GetType(clsErro), xRoot)Dim file As New StreamWriter(erroPath, True)writer.Serialize(file, erro)
file.Close()
End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickwriteErrorData("TestString", "Testing", "7")
End SubEnd
Class
Regards,
S_DS
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 "<" 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
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
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