CustomXMLNode.HasChildNodes 方法 (Office)

如果目前的元素節點具有子元素節點,會取得 True

語法

運算式HasChildNodes

運算式 會傳回 CustomXMLNode 物件的運算式。

傳回值

布林值

註解

CustomXMLNode 不是節點類型 msoCustomXMLNodeElement 時,此方法會永遠傳回 False

範例

下列範例會示範如何使用各種方法來新增自訂的 XML 元件、使用不同的準則來選取元件和節點、附加子樹系、測試是否已順利新增子樹,以及刪除元件和節點。

Sub ShowCustomXmlParts() 
    On Error GoTo Err 
 
    Dim cxps As CustomXMLParts 
    Dim cxp1 As CustomXMLPart 
    Dim cxp2 As CustomXMLPart 
    Dim cxn As CustomXMLNode 
    Dim cxns As CustomXMLNodes 
    Dim strXml As String 
    Dim strUri As String 
 
    With ActiveDocument 
        ' Example written for Word. 
 
        ' Adding a custom XML part. 
        .CustomXMLParts.Add "<custXMLPart />" 
         
        ' Add and then load from a file. 
        Set cxp1 = .CustomXMLParts.Add 
        cxp1.Load "c:\invoice.xml" 
         
        ' Returns the first custom XML part with the given root namespace. 
        Set cxp2 = .CustomXMLParts("urn:invoice:namespace")     '  
         
        ' Access all with a given root namespace; returns the entire collection. 
        Set cxps = .CustomXMLParts.SelectByNamespace("urn:invoice:namespace") 
         
        ' DOM-type operations. 
        ' Get the XML. 
        strXml = cxp2.XML 
        ' Get the root namespace. 
        strUri = cxp2.NamespaceURI  
        ' Get nodes using XPath.                              
        Set cxn = cxp2.SelectSingleNode("//*[@quantity < 4]")  
        Set cxns = cxp2.SelectNodes("//*[@unitPrice > 20]") 
        ' Append a child subtree to the single node selected previously. 
        cxn.AppendChildSubtree("<discounts><discount>0.10</discount></discounts>")   
 
         ' Tell user that the child subtree was added. 
         If  cxn.HasChildNodes then 
            MsgBox("The 'Discounts' nodes were added.")  
         Else 
            MsgBox("The 'Discounts' nodes were not added.")  
         End If          
         
        ' Delete custom XML part and node and its children. 
        cxp2.Delete 
        cxn.Delete 
 
                 
    End With 
     
    Exit Sub 
                 
' Exception handling. Show the message and resume. 
Err: 
        MsgBox (Err.Description) 
        Resume Next 
End Sub 

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應