Last post Mar 15, 2016 02:55 PM by uid250511
Mar 10, 2016 03:08 PM|uid250511|LINK
I am creating an XML programatically. Below is the starting tag which is then followed by looping through the data, adding in the required elements etc
Dim XDoc As XDocument = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customers></Customers>
To test the above XML file works i create a separate project
and everything works fine when there's data available. Occasionally when theres no data the XML page looks like the below in a browser
At this stage. when reading the XML URL, using the below code, the error "DTD must be defined before the document root element" is thrown.
Dim Xdoc As XDocument = XDocument.Load(UrlToXmlFile)
Although i can trap the error, i thought perhaps i may have done something wrong when creating the XML (Xml isnt my strong point). Some sites suggests adding a DTD (<!DOCTYPE note
SYSTEM "Note.dtd"> for example), i dont know if this is correct or if i can ignore the error or if there is a better way to to declare this?
Mar 11, 2016 06:29 AM|Chris Zhao|LINK
The standalone declaration indicates whether a document relies on information from an external source, such as external document type definition (DTD), for its content.
If the standalone declaration has a value of "yes", for example, <?xml version="1.0" standalone="yes"?>, the parser will report an error if the document references an external DTD or external entities.
Leaving out the standalone declaration produces the same result as including a standalone declaration of "no". The XML parser will accept external resources, if there are any, without reporting an error.
Mar 14, 2016 04:18 PM|uid250511|LINK
I tried the below lines and both caused the same error?
Dim XDoc As XDocument = <?xml version="1.0" encoding="UTF-8" standalone="no"?><Customers></Customers>
Dim XDoc As XDocument = <?xml version="1.0" encoding="UTF-8"?><Customers></Customers>
Am i doing something wrong?
Something i noticed is when viewing the XML in a browser i dont see any of the below declaration
<?xml version="1.0" encoding="UTF-8"?>
Dont know if this helps to narrow down the problem?
Mar 15, 2016 02:55 PM|uid250511|LINK
By adding the declaration it resolved the issue. There was a couple of ways of doing this either call the Declaration on the Xdoc and ToString or have a StringWriter and pass that to the Save method on the Xdoc and then GetBuilder.ToString