0

我正在尝试将 documentelement 设置为我想要的根元素。当我尝试这样做时,我得到以下错误属性'DocumentElement'是只读的。我想这是有道理的。我目前正在尝试将 msxml 转换为 system.xml。

Public Function GetXmlAllHierarchyObjects(Optional ByVal blnHideDisabled As Boolean = False) As XmlDocument

    Dim recHierarchy As Recordset
    Dim strSql As String = ""
    Dim xmlDoc As XmlDocument = New XmlDocument()
    Dim xmlRoot As XmlElement
    Dim xmlParent As XmlNode
    Dim intHeight As Integer
    Dim xmlHierarchy As XmlElement

    xmlRoot = xmlDoc.CreateElement("Hierarchy")
    xmlDoc.DocumentElement = xmlRoot ' Error occurs here
    recHierarchy = GetAllHierarchyObjects(blnHideDisabled, True)

    Do While Not recHierarchy.EOF
        xmlParent = xmlDoc.selectSingleNode("//Object[@ID='" & CStrSafe(recHierarchy("ParentID")) & "']")
        If xmlParent Is Nothing Then
            xmlParent = xmlRoot
            intHeight = 0
        Else
            intHeight = CIntSafe(xmlParent.SelectSingleNode("Height").InnerText) + 1
        End If

        xmlHierarchy = xmlDoc.createElement("Object")
        xmlParent.appendChild(xmlHierarchy)

        xmlHierarchy.SetAttribute("ID", recHierarchy("ObjectID").ToString())

        xmlHierarchy.appendChild(xmlDoc.createElement("Height"))
        xmlHierarchy.LastChild.InnerText = CStrSafe(intHeight)

        xmlHierarchy.appendChild(xmlDoc.createElement("ParentID"))
        xmlHierarchy.LastChild.InnerText = CStrSafe(recHierarchy("ParentID"))
        xmlHierarchy.appendChild(xmlDoc.createElement("Name"))
        xmlHierarchy.LastChild.InnerText = CStrSafe(recHierarchy("Name"))

        recHierarchy.MoveNext()
    Loop

    CloseRecordset(recHierarchy)

    GetXmlAllHierarchyObjects = xmlDoc

End Function
4

1 回答 1

1

文档元素并不是真正的根节点。它比根高一级。因此,您将根节点添加为它的子节点。

    Dim xmlDoc As XmlDocument = New XmlDocument()
    Dim xmlRoot As XmlElement = xmlDoc.CreateElement("Hierarchy")
    xmlDoc.AppendChild(xmlRoot)
    MsgBox(xmlDoc.OuterXml)
于 2016-09-24T10:36:14.120 回答