Question Root element is missing

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
I'm having some troubles passing XML through TCP/IP. I've tried doing this two ways and both times I get "Root element is missing"

Here is the XML I am trying to pass:
pzZp6Gg.png


At first I thought I would try building the XML and then passing it..so I just created each of the elements and attributes manually
VB.NET:
        Dim xml As New System.Xml.XmlDocument()

        Dim root As XmlElement
        root = xml.CreateElement("epp")
        xml.AppendChild(root)

        Dim xmlns As XmlAttribute = xml.CreateAttribute("xmlns")
        xmlns.Value = "urn:ietf:params:xml:ns:epp-1.0"
        root.SetAttributeNode(xmlns)

        Dim auth As XmlElement
        auth = xml.CreateElement("command")
        root.AppendChild(auth)

        Dim login As XmlElement
        login = xml.CreateElement("login")
        auth.AppendChild(login)

        Dim username As XmlElement
        username = xml.CreateElement("clID")
        username.InnerText = loginID
        login.AppendChild(username)

        Dim pass As XmlElement
        pass = xml.CreateElement("pw")
        pass.InnerText = password
        login.AppendChild(pass)

        Dim options As XmlElement
        options = xml.CreateElement("options")
        login.AppendChild(options)

        Dim version As XmlElement
        version = xml.CreateElement("version")
        version.InnerText = "1.0"
        options.AppendChild(version)

        Dim lang As XmlElement
        lang = xml.CreateElement("lang")
        lang.InnerText = "en"
        options.AppendChild(lang)

        Dim svcs As XmlElement
        svcs = xml.CreateElement("svcs")
        login.AppendChild(svcs)

        Dim objURI1 As XmlElement
        objURI1 = xml.CreateElement("objURI")
        objURI1.InnerText = "urn:ietf:params:xml:ns:contact-1.0"
        svcs.AppendChild(objURI1)

        Dim objURI2 As XmlElement
        objURI2 = xml.CreateElement("objURI")
        objURI2.InnerText = "urn:ietf:params:xml:ns:host-1.0"
        svcs.AppendChild(objURI2)

        Dim objURI3 As XmlElement
        objURI3 = xml.CreateElement("objURI")
        objURI3.InnerText = "urn:ietf:params:xml:ns:domain-1.0"
        svcs.AppendChild(objURI3)

        Dim svcExtension As XmlElement
        svcExtension = xml.CreateElement("svcExtension")
        svcs.AppendChild(svcExtension)

        Dim extURI As XmlElement
        extURI = xml.CreateElement("extURI")
        extURI.InnerText = "http://rxsd.domain-registry.nl/sidn-ext-epp-1.0"
        svcExtension.AppendChild(extURI)

After trying it and receiving the missing root error I looked at xml.OuterXML and noticed the <?xml version="1.0" encoding="UTF-8" standalone="no"?> was missing. That's when I decided to try making a string and including it, then passing it that way:
VB.NET:
Dim post As String = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?><epp xmlns=""urn:ietf:params:xml:ns:epp-1.0""><command><login><clID>" & loginID & "</clID><pw>" & password & "</pw><options><version>1.0</version><lang>en</lang></options><svcs><objURI>urn:ietf:params:xml:ns:contact-1.0</objURI><objURI>urn:ietf:params:xml:ns:host-1.0</objURI><objURI>urn:ietf:params:xml:ns:domain-1.0</objURI><svcExtension><extURI>http://rxsd.domain-registry.nl/sidn-ext-epp-1.0</extURI></svcExtension></svcs></login><clTRID>300100</clTRID></command></epp>"

Still got the same error... Any ideas? Here is my code to connect and pass the XML..
VB.NET:
Dim client As New TcpClient("drs.registry.nl", 700)
Dim ns As NetworkStream = client.GetStream

            If ns.CanRead And ns.CanWrite Then
                Dim sendBytes As Byte() = Encoding.UTF8.GetBytes(post)
                ns.Write(sendBytes, 0, sendBytes.Length)

                Dim responseBytes(client.ReceiveBufferSize) As Byte
                ns.Read(responseBytes, 0, CInt(client.ReceiveBufferSize))

                Dim xResponse As XmlDocument
                xResponse = New XmlDocument
                xResponse.LoadXml(Encoding.UTF8.GetString(responseBytes))
                MsgBox(Encoding.UTF8.GetString(responseBytes))
            Else
                MsgBox("Cannot Write to Stream (Login)")
                Return False
            End If
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Just an update..I made some changes and I now receive a response, but it's blank. The XML needs to be sent securely so I added SSLstream into the code as well.

Any ideas why this would be returning nothing?

VB.NET:
        Try
            Dim sb As New StringBuilder()
            sb.Append("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>")
            sb.Append("<epp xmlns=""urn:ietf:params:xml:ns:epp-1.0"">")
            sb.Append("<command>")
            sb.Append("<login>")
            sb.Append("<clID>" & loginID & "</clID>")
            sb.Append("<pw>" & password & "</pw>")
            sb.Append("<options>")
            sb.Append("<version>1.0</version>")
            sb.Append("<lang>en</lang>")
            sb.Append("</options>")
            sb.Append("<svcs>")
            sb.Append("<objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>")
            sb.Append("<objURI>urn:ietf:params:xml:ns:host-1.0</objURI>")
            sb.Append("<objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>")
            sb.Append("<svcExtension>")
            sb.Append("<extURI>http://rxsd.domain-registry.nl/sidn-ext-epp-1.0</extURI>")
            sb.Append("</svcExtension>")
            sb.Append("</svcs>")
            sb.Append("</login>")
            sb.Append("</command>")
            sb.Append("</epp>")

            Dim client As New TcpClient("drs.domain-registry.nl", 700)
            Dim sslStream As New Security.SslStream(client.GetStream(), False)
            sslStream.AuthenticateAsClient("drs.domain-registry.nl")
            Dim messsage As Byte() = Encoding.UTF8.GetBytes(sb.ToString)
            sslStream.Write(messsage)
            sslStream.Flush()

            'get the response
            Dim serverMessage As String = ReadMessage(sslStream)

            MsgBox("Login: " & serverMessage.ToString)

            client.Close()
        Catch ex As Exception
            MsgBox("Exception: " & ex.Message)
            Return False
        End Try

VB.NET:
    Private Shared Function ReadMessage(ByVal sslStream As Security.SslStream) As String
        ' Read the  message sent by the server.
        ' The end of the message is signaled using the
        ' "</epp>" marker.
        Dim buffer As Byte() = New Byte(2047) {}
        Dim messageData As New StringBuilder()
        Dim bytes As Integer = -1
        Do
            bytes = sslStream.Read(buffer, 0, buffer.Length)

            ' Use Decoder class to convert from bytes to UTF8
            ' in case a character spans two buffers.
            Dim decoder As Decoder = Encoding.UTF8.GetDecoder()
            Dim chars As Char() = New Char(decoder.GetCharCount(buffer, 0, bytes) - 1) {}
            decoder.GetChars(buffer, 0, bytes, chars, 0)
            messageData.Append(chars)
            ' Check for </epp>.
            If messageData.ToString().IndexOf("</epp>") <> -1 Then
                Exit Do
            End If
        Loop While bytes <> 0

        Return messageData.ToString()
    End Function

Thanks in advance!
 
Last edited:

WiVM

New member
Joined
Dec 25, 2015
Messages
1
Programming Experience
Beginner
Did you find a solution to this problem? I have the same issue. The login message is received, but the response to that message is nothing.
 
Top Bottom