sadshead
asked on
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("be ans", "-//SPRING//DTD BEAN//EN", "http://www.######/dtd/######-beans.dtd", Nothing)
It works, any ideas?
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("be
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"
The URL specified in systemID parameter of CreateDocumentType has to point to valid external document type definitions.
ASKER
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.
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.
>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.
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.
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.
ASKER
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("be ans", "-//SPRING//DTD BEAN//EN", "", Nothing)
it goes through fine so its the http:// bit
"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("be
it goes through fine so its the http:// bit
>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.
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)
ASKER
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. "
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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'.
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'.
ASKER
opens up and is correct in notepad but generates the above error if opened in IE
If you are using #### to hide the actual URL, try using the proper URL instead in your code.
doctype = doc.CreateDocumentType("be ans", "-//SPRING//DTD BEAN//EN", "http://www.springframework.org/dtd/spring-beans.dtd", Nothing)
This doesn't create the error....
This doesn't create the error....
ASKER
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.
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)
ASKER
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
Dim doctypestringURL As String = "<!DOCTYPE beans PUBLIC ""-//SPRING//DTD BEAN//EN"" ""http://www.springframework.org/dtd/spring-beans.dtd"">"
Same outcome.
Steve
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)
ASKER
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
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
ASKER
I have debugged the following line:
My.Computer.FileSystem.Wri teAllText( xmlPath, doc.InnerXml.Replace(docty pestring, 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"" """">)
My.Computer.FileSystem.Wri
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"" """">)
>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.
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.
ASKER
... 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:
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)
>... 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.
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.
ASKER
As an example:
xmlpath = D:\Document and Settings\username\visual studio 2005\xml\snon--20-01-10.tx t
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("be ans", "-//SPRING//DTD BEAN//EN", Nothing, Nothing) apart from the quotation marks (").
xmlpath = D:\Document and Settings\username\visual studio 2005\xml\snon--20-01-10.tx
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("be
Use this to check:
MsgBox(doc.InnerXml.Contai ns(doctype string))
Should be true.
MsgBox(doc.InnerXml.Contai
Should be true.
ASKER
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.Fi leSystemOb ject")
'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!
Dim fso, inputFile, outputFile
Dim str As String
fso = CreateObject("Scripting.Fi
'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
outputFile.Write(str)
outputFile.close()
Many thanks for all your help and assistance Paul i wouldnt have got there without you!
Glad you got it sorted... I'm still confused why it wasn't working from the .NET environment.
ASKER
Open in new window