1

XmlDocument クラスの作成に問題があります。これは私がやろうとしたことです:

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)

これにより、次のエラーが発生します。指定された場所にノードを挿入できません。このエラーをスローする行はmyDoc.InsertBefore(xmldecl, root)

これを理解することはできません。これらの要素をどの順序で挿入する必要がありますか? 私はさまざまな注文を試しましたが、私は何か完全に間違ったことをしているだけだと思います.これはそもそもうまくいかないはずです:)しかし、どうすればこれを行うことができますか?

4

1 に答える 1

1

これは私のために働きます:

Dim myDoc As New XmlDocument()
Dim xmldecl As XmlDeclaration = myDoc.CreateXmlDeclaration("1.0", Encoding.GetEncoding("ISO-8859-15").BodyName, "yes")
myDoc.AppendChild(xmldecl)
Dim docType As XmlDocumentType = myDoc.CreateDocumentType("DtdAttribute", Nothing, "DtdFile.dtd", Nothing)
myDoc.XmlResolver = Nothing
myDoc.AppendChild(docType)
Dim root As XmlElement = myDoc.CreateElement("DtdAttribute")
myDoc.AppendChild(root)

nameルート要素名は、に指定されたパラメータと同じである必要があることに注意してくださいXmlDocument.CreateDocumentType

XmlTextWriterただし、このようにXMLドキュメントを最初から作成する場合は、 :を使用する方が簡単です。

Using writer As New XmlTextWriter("C:\Test.xml", Encoding.GetEncoding("ISO-8859-15"))
    writer.WriteStartDocument()
    writer.WriteDocType("DtdAttribute", Nothing, "DtdFile.dtd", Nothing)
    writer.WriteStartElement("DtdAttribute")
    writer.WriteEndElement()
    writer.WriteEndDocument()
End Using
于 2012-12-18T12:58:18.133 に答える