XML Generation wont allow me to add line (DTD)

If i run the attached code i get:

Server Error in '/Fixedline' Application.
--------------------------------------------------------------------------------

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 63.246.7.75:80

However if i remove the line:

doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", "http://www.######/dtd/######-beans.dtd", Nothing)

It works, any ideas?
Dim xmldecl As XmlDeclaration
            xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
            xmldecl.Encoding = "UTF-8"
            xmldecl.Standalone = ""
            doc.AppendChild(xmldecl)
            ' Create DocType line
            Dim doctype As XmlDocumentType
            Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")
            Dim xmldecl As XmlDeclaration
            xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
            xmldecl.Encoding = "UTF-8"
            xmldecl.Standalone = ""
            doc.AppendChild(xmldecl)
            ' Create DocType line
            Dim doctype As XmlDocumentType
            Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")
            doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", "http://www.######/dtd/######-beans.dtd", Nothing)
            doctype = doc.CreateDocumentType("beans", Nothing, Nothing, Nothing)
            doc.AppendChild(doctype)
            ' Create Comment Line
            Dim comment As XmlNode = doc.CreateComment("Devices")
            doc.AppendChild(comment)
            Dim bookNode As XmlNode = CreateBookNode(doc)
            Dim bookNode2 As XmlNode = CreateBookNode2(doc)
            Dim bookNode3 As XmlNode = CreateBookNode3(doc)
            'Append the book node to the bookstore node 
            bookstoreNode__2.AppendChild(bookNode)
            bookstoreNode__2.AppendChild(bookNode2)
            bookstoreNode__2.AppendChild(bookNode3)
            'Append the bookstore node to the document 
            doc.AppendChild(bookstoreNode__2)
            lblResult.Text = "XML Document has been successfully created"
            doctype = doc.CreateDocumentType("beans", Nothing, Nothing, Nothing)
            doc.AppendChild(doctype)
            ' Create Comment Line
            Dim comment As XmlNode = doc.CreateComment("Devices")
            doc.AppendChild(comment)
            Dim bookNode As XmlNode = CreateBookNode(doc)
            Dim bookNode2 As XmlNode = CreateBookNode2(doc)
            Dim bookNode3 As XmlNode = CreateBookNode3(doc)
            'Append the book node to the bookstore node 
            bookstoreNode__2.AppendChild(bookNode)
            bookstoreNode__2.AppendChild(bookNode2)
            bookstoreNode__2.AppendChild(bookNode3)
            'Append the bookstore node to the document 
            doc.AppendChild(bookstoreNode__2)
            lblResult.Text = "XML Document has been successfully created"

Open in new window

LVL 1
sadsheadAsked:
Who is Participating?
 
PaulHewsConnect With a Mentor Commented:
>I'm now getting "This document already has a 'DocumentElement' node. "

This means you are adding multiple root nodes.  You pasted my code in without understanding what it was doing.  What I specified in my code should be the very last thing you do, because you can't manipulate the document any more afterwards.


Dim xmldecl As XmlDeclaration
        xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
        xmldecl.Encoding = "UTF-8"
        xmldecl.Standalone = ""
        doc.AppendChild(xmldecl)
        ' Create DocType line
        Dim doctype As XmlDocumentType
        Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")

        doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing)

        doc.AppendChild(doctype)
        
        'String replacement to add URL to doctype 
       
        ' Create Comment Line
        Dim comment As XmlNode = doc.CreateComment("Devices")
        doc.AppendChild(comment)
        Dim bookNode As XmlNode = CreateBookNode(doc)
        Dim bookNode2 As XmlNode = CreateBookNode2(doc)
        Dim bookNode3 As XmlNode = CreateBookNode3(doc)
        'Append the book node to the bookstore node 
        bookstoreNode__2.AppendChild(bookNode)
        bookstoreNode__2.AppendChild(bookNode2)
        bookstoreNode__2.AppendChild(bookNode3)
        'Append the bookstore node to the document 
        doc.AppendChild(bookstoreNode__2)
        lblResult.Text = "XML Document has been successfully created"

        Dim doctypestring As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">"
        Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.#######.org/dtd/spring-beans.dtd"">"
        'We can't parse the document with the changed doctype, because it will result in the same error.   
        'So instead, write the XML to file or to stream or whereever it needs to go 
        My.Computer.FileSystem.WriteAllText(xmlpath, doc.InnerXml.Replace(doctypestring, doctypestringURL), False)

Open in new window

0
 
sadsheadAuthor Commented:
Full error message:
Server Error in '/knowledge/Applications/FixedLine' Application.
--------------------------------------------------------------------------------

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Source Error: 


Line 41:             Dim doctype As XmlDocumentType
Line 42:             Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")
Line 43:             doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", "http://www.###########.org/dtd/######-beans.dtd", Nothing)
Line 44:             doctype = doc.CreateDocumentType("beans", Nothing, Nothing, Nothing)
Line 45:             doc.AppendChild(doctype)
 

Source File: D:\Inetpub\CTi\knowledge\applications\fixedline\onenet\xml\Default.aspx.vb    Line: 43 

Stack Trace: 


[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond]
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +1028066
   System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +33
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +431

[WebException: Unable to connect to the remote server]
   System.Net.HttpWebRequest.GetResponse() +1529755
   System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +61
   System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +1865280
   System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +51
   System.Xml.XmlTextReaderImpl.OpenStream(Uri uri) +30
   System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +382
   System.Xml.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId) +16
   System.Xml.DtdParser.ParseExternalSubset() +18
   System.Xml.DtdParser.ParseFreeFloatingDtd() +54
   System.Xml.DtdParser.Parse(Boolean saveInternalSubset) +1331362
   System.Xml.DtdParser.Parse(XmlNameTable nt, XmlNamespaceManager nsManager, Boolean namespaces, String baseUri, String docTypeName, String publicId, String systemId, String internalSubset, Boolean useResolver, XmlResolver resolver) +239
   System.Xml.XmlLoader.ParseDocumentType(XmlDocumentType dtNode, Boolean bUseResolver, XmlResolver resolver) +105
   System.Xml.XmlLoader.ParseDocumentType(XmlDocumentType dtNode) +54
   System.Xml.XmlDocumentType..ctor(String name, String publicId, String systemId, String internalSubset, XmlDocument doc) +205
   System.Xml.XmlDocument.CreateDocumentType(String name, String publicId, String systemId, String internalSubset) +40
   onenet_xml_Default.btnSave_Click(Object sender, EventArgs e) in D:\Inetpub\CTi\knowledge\applications\fixedline\onenet\xml\Default.aspx.vb:43
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832

Open in new window

0
 
PaulHewsCommented:
The URL specified in systemID parameter of CreateDocumentType has to point to valid external document type definitions.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sadsheadAuthor Commented:
I am new to XML what does the DTD file do and why do i need it?

Basically i have written this script to create a document automatically, where as we normally pay a third party to create it for us. This is the only line that i cant add.
0
 
PaulHewsCommented:
>I am new to XML what does the DTD file do and why do i need it?

The document type definition contains schema information for the XML file.  Whether you need it or not depends on who or what is the consumer of your XML, and whether it can be consumed without that extra layer of schema validation.  
0
 
PaulHewsCommented:
In essence, I would try leaving it out and seeing if you can still use the resulting document.  If the URL is non-functional, then it shouldn't actually be providing any extra schema validation anyway.
0
 
sadsheadAuthor Commented:
Apparently that line must be in the outputted XML document but whenever i try to add it in i get:

"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ##.246.7.75:80"

If i change it to:

doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", "", Nothing)

it goes through fine so its the http:// bit
0
 
PaulHewsCommented:
>it goes through fine so its the http:// bit

That is what I said in http:#a26306368  That has to be a valid URL.

Either leave it out, or add it in through string manipulation, as in this sample.


doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing)

doc.AppendChild(doctype)
doc.AppendChild(doc.CreateElement("root"))

'String replacement to add URL to doctype
Dim doctypestring As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">"
Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.###########.org/dtd/######-beans.dtd"">"
'We can't parse the document with the changed doctype, because it will result in the same error.  
'So instead, write the XML to file or to stream or whereever it needs to go
My.Computer.FileSystem.WriteAllText("C:\temp\test.xml", doc.InnerXml.Replace(doctypestring, doctypestringURL), False)

Open in new window

0
 
sadsheadAuthor Commented:
Thanks Paul,
This looks like exactly what i need i have added it into my code as per the code attached below

I'm now getting "This document already has a 'DocumentElement' node. "

            Dim xmldecl As XmlDeclaration
            xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
            xmldecl.Encoding = "UTF-8"
            xmldecl.Standalone = ""
            doc.AppendChild(xmldecl)
            ' Create DocType line
            Dim doctype As XmlDocumentType
            Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")
           
            doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing)

            doc.AppendChild(doctype)
            doc.AppendChild(doc.CreateElement("root"))

            'String replacement to add URL to doctype 
            Dim doctypestring As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">"
            Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.#######.org/dtd/spring-beans.dtd"">"
            'We can't parse the document with the changed doctype, because it will result in the same error.   
            'So instead, write the XML to file or to stream or whereever it needs to go 
            My.Computer.FileSystem.WriteAllText("C:\temp\test.xml", doc.InnerXml.Replace(doctypestring, doctypestringURL), False)
            ' Create Comment Line
            Dim comment As XmlNode = doc.CreateComment("Devices")
            doc.AppendChild(comment)
            Dim bookNode As XmlNode = CreateBookNode(doc)
            Dim bookNode2 As XmlNode = CreateBookNode2(doc)
            Dim bookNode3 As XmlNode = CreateBookNode3(doc)
            'Append the book node to the bookstore node 
            bookstoreNode__2.AppendChild(bookNode)
            bookstoreNode__2.AppendChild(bookNode2)
            bookstoreNode__2.AppendChild(bookNode3)
            'Append the bookstore node to the document 
            doc.AppendChild(bookstoreNode__2)
            lblResult.Text = "XML Document has been successfully created"
        doc.Save(xmlPath)

Open in new window

0
 
sadsheadAuthor Commented:
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
An XML element is not allowed inside a DTD. Error processing resource 'http://www./#######.org/dtd/spring-beans.dtd'.
0
 
sadsheadAuthor Commented:
opens up and is correct in notepad but generates the above error if opened in IE
0
 
PaulHewsCommented:
If you are using #### to hide the actual URL, try using the proper URL instead in your code.
0
 
PaulHewsCommented:
doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", "http://www.springframework.org/dtd/spring-beans.dtd", Nothing)

This doesn't create the error....
0
 
sadsheadAuthor Commented:
Hi Paul
I have attached my current code... appologies for the delay in following this up.

What i am now getting in my xml document is:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">

Many thanks for all your assistance so far.



           Dim xmldecl As XmlDeclaration
            xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
            xmldecl.Encoding = "UTF-8"
            xmldecl.Standalone = ""
            doc.AppendChild(xmldecl)
            ' Create DocType line
            Dim doctype As XmlDocumentType
            Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")

            doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing)

            doc.AppendChild(doctype)


            'String replacement to add URL to doctype 

            ' Create Comment Line
            Dim comment As XmlNode = doc.CreateComment("Devices")
            doc.AppendChild(comment)
            Dim bookNode As XmlNode = CreateBookNode(doc)
            Dim bookNode2 As XmlNode = CreateBookNode2(doc)
            Dim bookNode3 As XmlNode = CreateBookNode3(doc)
            'Append the book node to the bookstore node 
            bookstoreNode__2.AppendChild(bookNode)
            bookstoreNode__2.AppendChild(bookNode2)
            bookstoreNode__2.AppendChild(bookNode3)
            'Append the bookstore node to the document 
            doc.AppendChild(bookstoreNode__2)
            lblResult.Text = "XML Document has been successfully created"

            Dim doctypestring As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">"
            Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.#######.org/dtd/spring-beans.dtd"">"
            'We can't parse the document with the changed doctype, because it will result in the same error.   
            'So instead, write the XML to file or to stream or whereever it needs to go 
            My.Computer.FileSystem.WriteAllText(xmlPath, doc.InnerXml.Replace(doctypestring, doctypestringURL), False)

Open in new window

0
 
sadsheadAuthor Commented:
Apologies Line 32 should read:

Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.springframework.org/dtd/spring-beans.dtd"">"

Same outcome.
 
Steve
0
 
PaulHewsCommented:
What are the problems when you use this?
Dim xmldecl As XmlDeclaration
xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
xmldecl.Encoding = "UTF-8"
xmldecl.Standalone = ""
doc.AppendChild(xmldecl)
' Create DocType line
Dim doctype As XmlDocumentType
Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")
doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", "http://www.springframework.org/dtd/spring-beans.dtd", Nothing)

doc.AppendChild(doctype)

' Create Comment Line
Dim comment As XmlNode = doc.CreateComment("Devices")
doc.AppendChild(comment)
Dim bookNode As XmlNode = CreateBookNode(doc)
Dim bookNode2 As XmlNode = CreateBookNode2(doc)
Dim bookNode3 As XmlNode = CreateBookNode3(doc)
'Append the book node to the bookstore node 
bookstoreNode__2.AppendChild(bookNode)
bookstoreNode__2.AppendChild(bookNode2)
bookstoreNode__2.AppendChild(bookNode3)
'Append the bookstore node to the document 
doc.AppendChild(bookstoreNode__2)
lblResult.Text = "XML Document has been successfully created"
doc.Save(xmlPath)

Open in new window

0
 
sadsheadAuthor Commented:
The same as i had originally:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 63.###.7.75:80
0
 
sadsheadAuthor Commented:
I have debugged the following line:


My.Computer.FileSystem.WriteAllText(xmlPath, doc.InnerXml.Replace(doctypestring, doctypestringURL), False)
And the value of  doctypestringURL is:
"<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"http://www.springframework.org/dtd/spring-beans.dtd">"
 
So its either not doc.InnerXml.Replacing or not WriteAllTexting i would assume the latter as what is written to the resulting file is effectivly doctypestring (<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">)
0
 
PaulHewsCommented:
>The same as i had originally:

That's an issue with connectivity with that address.  It works fine here.

The string replace code I posted above works, as I tested it.
0
 
sadsheadAuthor Commented:
... but i can access that address if i paste it into my browser, i even added msgbox's in to display xmlpath, doctypestring and doctypestringURL all look absolutly fine.
I have tried a different URL and it still doesnt work!

Heres my code again:



Dim xmldecl As XmlDeclaration
            xmldecl = doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
            xmldecl.Encoding = "UTF-8"
            xmldecl.Standalone = ""
            doc.AppendChild(xmldecl)
            ' Create DocType line
            Dim doctype As XmlDocumentType
            Dim bookstoreNode__2 As XmlNode = doc.CreateElement("beans")

            doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing)

            doc.AppendChild(doctype)

            'String replacement to add URL to doctype 

            ' Create Comment Line
            Dim comment As XmlNode = doc.CreateComment("Devices")
            doc.AppendChild(comment)
            Dim bookNode As XmlNode = CreateBookNode(doc)
            Dim bookNode2 As XmlNode = CreateBookNode2(doc)
            Dim bookNode3 As XmlNode = CreateBookNode3(doc)
            'Append the book node to the bookstore node 
            bookstoreNode__2.AppendChild(bookNode)
            bookstoreNode__2.AppendChild(bookNode2)
            bookstoreNode__2.AppendChild(bookNode3)
            'Append the bookstore node to the document 
            doc.AppendChild(bookstoreNode__2)
            lblResult.Text = "XML Document has been successfully created"
            Dim doctypestring As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">"
            Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.bbc.co.uk"">"
            MsgBox(xmlPath)
            MsgBox(doctypestring)
            MsgBox(doctypestringURL)
            My.Computer.FileSystem.WriteAllText(xmlPath, doc.InnerXml.Replace(doctypestring, doctypestringURL), False)

Open in new window

0
 
PaulHewsCommented:
>... but i can access that address if i paste it into my browser

Likely a configuration problem with your system then.  Browser could be accessing the web via proxy, or security software blocking your application's network access.

The replacement code only works if the search string is contained properly in the document XML.  Do a debug.print for doc.InnerXML and compare the doctypestring variable with what is declared as the doctype within that.

If you are looking in Notepad, and the XML is properly formed, but it fails in IE, that could go back to the connectivity issue.
0
 
sadsheadAuthor Commented:
As an example:

xmlpath = D:\Document and Settings\username\visual studio 2005\xml\snon--20-01-10.txt

doctypestring=<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">

doctypestringURL =<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.bbc.co.uk">

The resulting txt file when open in notepad displays the line as <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">

I think you may be correct and that it is not finding doctypestring to match anyline in the document created but i cannot see any difference between <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""> and doctype = doc.CreateDocumentType("beans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing) apart from the quotation marks (").

0
 
PaulHewsCommented:
Use this to check:

MsgBox(doc.InnerXml.Contains(doctypestring))

Should be true.
0
 
sadsheadAuthor Commented:
I decied to look at a different way of replacing the string in the txt document and got it working:

        Dim fso, inputFile, outputFile
        Dim str As String
        fso = CreateObject("Scripting.FileSystemObject")
        '1 means for reading
        inputFile = fso.OpenTextFile(xmlPath, 1)
        str = inputFile.ReadAll
        str = Replace(str, "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" """">", "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.springframework.org/dtd/spring-beans.dtd"">")
        inputFile.Close()
        'write back
        outputFile = fso.CreateTextFile(xmlPath, True)
        outputFile.Write(str)
        outputFile.close()

Many thanks for all your help and assistance Paul i wouldnt have got there without you!
0
 
PaulHewsCommented:
Glad you got it sorted... I'm still confused why it wasn't working from the .NET environment.
0
All Courses

From novice to tech pro — start learning today.