Foros del Web » Programación para mayores de 30 ;) » .NET »

Creacion de xml

Estas en el tema de Creacion de xml en el foro de .NET en Foros del Web. No se mucho como se crea un xml, pero encontre este codigo y me funciona muy bien, pero cuando me pidieron almacenar el archivo xml ...
  #1 (permalink)  
Antiguo 28/09/2011, 07:04
Avatar de Alishta7  
Fecha de Ingreso: junio-2009
Mensajes: 25
Antigüedad: 14 años, 9 meses
Puntos: 0
Creacion de xml

No se mucho como se crea un xml, pero encontre este codigo y me funciona muy bien, pero cuando me pidieron almacenar el archivo xml en una carpeta, me da error.

Código:
<WebMethod()> _
    Public Sub myThrow2()

        'Contruye el Detail de los elementos del SOAP fault.
        Dim doc As New System.Xml.XmlDocument()
        Dim node As System.Xml.XmlNode = _
            doc.CreateNode(XmlNodeType.Element, _
            SoapException.DetailElementName.Name, _
            SoapException.DetailElementName.Namespace)

        Dim LogPath As String = Path() + "\" + NameFile().ToString().ToUpper()

        'Construye las especificaciones details del SoapException.
        'Agrega el primer hijo del elemento XML detail.
        Dim details As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1", "http://tempuri.org/")

        'Agrega un segundo hijo del elemento XML detail con un atributo.
        Dim details2 As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2", "http://tempuri.org/")
        Dim attr As XmlAttribute = doc.CreateAttribute("t", "attrName", "http://tempuri.org/")
        attr.Value = "attrValue"
        details2.Attributes.Append(attr)

        'Agrega el primer hijo del elemento XML detail.
        Dim details3 As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, "FaulType", "http://tempuri.org/")
        details3.InnerXml = "Valor del detalle"

        'Añade los dos elementos hijos del nodo detail.
        node.AppendChild(details)
        node.AppendChild(details2)
        node.AppendChild(details3)

        doc.Save(LogPath)

    End Sub
Y al ejecutarlo devuelve lo siguiente:

"System.Xml.XmlException: Este documento XML no es valido, El documento no tiene un elemento raiz..
en System.Xml.XmlDocument.Save(String filename)"


¿Que error estoy cometiendo?

:(
  #2 (permalink)  
Antiguo 28/09/2011, 07:11
Avatar de Alishta7  
Fecha de Ingreso: junio-2009
Mensajes: 25
Antigüedad: 14 años, 9 meses
Puntos: 0
Respuesta: Creacion de xml

Ya me di cuenta del error xDUU

me falto doc.AppendChild(node)

ahora si funciona :)

Código:
<WebMethod()> _
    Public Sub myThrow2()

        'Contruye el Detail de los elementos del SOAP fault.
        Dim doc As New System.Xml.XmlDocument()
        Dim node As System.Xml.XmlNode = _
            doc.CreateNode(XmlNodeType.Element, _
            SoapException.DetailElementName.Name, _
            SoapException.DetailElementName.Namespace)

            doc.AppendChild(node)

        Dim LogPath As String = Path() + "\" + NameFile().ToString().ToUpper()

        'Construye las especificaciones details del SoapException.
        'Agrega el primer hijo del elemento XML detail.
        Dim details As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1", "http://tempuri.org/")

        'Agrega un segundo hijo del elemento XML detail con un atributo.
        Dim details2 As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2", "http://tempuri.org/")
        Dim attr As XmlAttribute = doc.CreateAttribute("t", "attrName", "http://tempuri.org/")
        attr.Value = "attrValue"
        details2.Attributes.Append(attr)

        'Agrega el primer hijo del elemento XML detail.
        Dim details3 As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, "FaulType", "http://tempuri.org/")
        details3.InnerXml = "Valor del detalle"

        'Añade los dos elementos hijos del nodo detail.
        node.AppendChild(details)
        node.AppendChild(details2)
        node.AppendChild(details3)

        doc.Save(LogPath)

    End Sub

Última edición por Alishta7; 19/10/2011 a las 10:55

Etiquetas: xml
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:45.