Estoy creando un código html mediante xmlDocument. Me gustaría ver el código en árbol pero se muestra lineal. Aquí dejo un código de ejemplo:
'Crear un nuevo documento XML.
Dim xmlDoc As XmlDocument = New XmlDocument
'Crear la etiqueta html.
Dim xmlRoot As XmlElement = xmlDoc.CreateElement("html")
xmlDoc.AppendChild(xmlRoot)
'Crea la etiqueta de encabezado y agrégala al elemento html.
Dim xmlHead As XmlElement = xmlDoc.CreateElement("head")
xmlRoot.AppendChild(xmlHead)
'Crea la etiqueta del título, configura su texto como "Tabla de base de datos"
'y anexarlo debajo del elemento cabeza.
Dim xmlTitle As XmlElement = xmlDoc.CreateElement("title")
xmlTitle.AppendChild(xmlDoc.CreateTextNode("tabla de prueba"))
xmlHead.AppendChild(xmlTitle)
Dim xmlCSSStyle As XmlElement = xmlDoc.CreateElement("style")
xmlCSSStyle.AppendChild(xmlDoc.CreateTextNode("td{background-color: olive;}"))
xmlHead.AppendChild(xmlCSSStyle)
' <html>
' <head>
' <title> Tabla de base de datos </title>
' <style>
' td{background-color: olive;}
' </style>
' </head>
'</html>
' Create the body element and append it to the root.
Dim xmlBody As XmlElement = xmlDoc.CreateElement("body")
xmlRoot.AppendChild(xmlBody)
' Create the table and append it.
Dim xmlTable As XmlElement = xmlDoc.CreateElement("table")
xmlBody.AppendChild(xmlTable)
'' Create the rows.
For I = 1 To 2
Dim xmlRow As XmlElement = xmlDoc.CreateElement("tr")
xmlTable.AppendChild(xmlRow)
Dim xmlCell As XmlElement = xmlDoc.CreateElement("td")
xmlCell.AppendChild(xmlDoc.CreateTextNode("contenido"))
xmlRow.AppendChild(xmlCell)
Next
WebBrowser1.DocumentText = xmlDoc.OuterXml
Así es como queda el código HTML
Me ayudaría se visualizara así:
<!DOCTYPE html>
gracias de antemano
Tras mucho buscar encontré la manera, usando
XDocument en lugar de
XmlDocument. El código es muy flexible así que se puede hacer de muchas maneras, incluso a partir de un DataTable crear una tabla en HTML.
Aquí dejo un ejemplo sencillo de como lo voy a hacer:
Dim docHTML As XDocument = New XDocument
Dim docType As XDocumentType = New XDocumentType("html", Nothing, Nothing, Nothing)
docHTML.Add(docType) '<!DOCTYPE html >
Dim HTML As XElement = New XElement("html", New XElement("head", New XElement("title", "HTML de prueba")), New XElement("body"))
Dim HEAD As XElement = HTML.Element("head")
Dim BODY As XElement = HTML.Element("body")
Dim etiquetaStyle As XElement = New XElement("style", vbCrLf,
"body{background-color: red;}", vbCrLf,
"h1{font-style: italic}", vbCrLf)
Dim script As XElement = New XElement("script", "")
HEAD.Add(New XElement(etiquetaStyle))
Dim h1 As XElement = New XElement("h1", "Hola mundo")
h1.SetAttributeValue("class", "titulo1")
BODY.Add(New XElement(h1))
docHTML.Add(HTML)
TextBox1.Text = docHTML.ToString()
WebBrowser1.DocumentText = docHTML.ToString()
Esto quedaría así:
<!DOCTYPE html >
body{background-color: red;}
h1{font-style: italic}
También se puede escribir concatenado tal como xElement("html", new xElement("head",new xElement...), xElement("body", new xElement...), pero a la hora de modificar cosas cuando es muy largo se hace tedioso. Prefiero hacerlo por partes.
Para añadir atributos a una etiqueta basta usar
SetAttributeValue:
h1.SetAttributeValue("class", "titulo1")
y el resultado sería :
<h1 class="titulo1">Hola mundo
</h1>
Espero le sirvan saludos
Acabo de averiguar que es posible crear las etiquetas de la siguiente manera:
Dim TABLECLAS1 As XElement = New XElement(<table>
<tr>
<td>Mercurio</td>
<td>Venus</td>
</tr>
<tr>
<td>Mercurio</td>
<td>Venus</td>
</tr>
</table>)
Me ha parecido muy curioso así que posteo.