I have some trouble creating my XmlDocument class. This is what I’ve tried to do:
Dim myDoc = New XmlDocument()
Dim docType As XmlDocumentType = myDoc.CreateDocumentType("DtdAttribute", Nothing, "DtdFile.dtd", Nothing)
myDoc.XmlResolver = Nothing
myDoc.AppendChild(docType)
Dim xmldecl As XmlDeclaration = myDoc.CreateXmlDeclaration("1.0", Encoding.GetEncoding("ISO-8859-15").BodyName, "yes")
Dim root As XmlElement = myDoc.CreateElement("RootElement")
myDoc.AppendChild(root)
myDoc.InsertBefore(xmldecl, root)
This will result in error: Cannot insert the node in the specified location. Line throwing this error is myDoc.InsertBefore(xmldecl, root)
Just cannot figure out this. Which order should I insert these elements? I’ve tried different orders but I think I’m just doing something totally wrong and this shouldn’t even work in the first place 🙂 But then how to do this?
This works for me:
Note that the root element name must be the same as the
nameparameter given toXmlDocument.CreateDocumentType.You may find, however, that for building an XML document from scratch like this, it’s easier to just use the
XmlTextWriter: