如何解析父XML子节点和子XML节点属性

时间:2014-09-26 15:57:55

标签: .net xml parent-child nodes

我试图从XML文件中获取特定属性值,然后将它们放入表中。

以下是XML文件的摘录:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CUSTOMER defined="true">
<FIREWALLS defined="true">
<CMA display_name="JAPAN" ssh_port="" secondary_host_ip="" collector="Collector_A"      communication="cpstat" Allow_Auto_Implementation="no" secondary_host_name=""   secondary_host_sic_name="" monitoring="yes" audit_from_clm="no" log_collection_frequency="60"   type="CMA" name="" host_name="" use_opsec_data_collection="yes" user_name="" passwd=""   use_opsec_lea="yes" os="linux" defined="true">
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_A" name="FW_A"  display_name="FW_A" os="sun" user_name="" passwd="" log_collection_mode="extensive" baseline_profile="" host_name="10.10.10.1" log_server="JAPAN_Pry"/>
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_B" name="FW_B" display_name="FW_B" os="sun" user_name="" passwd="" baseline_profile="" log_collection_mode="extensive" host_name="10.10.10.2" log_server="JAPAN_Pry"/>
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_C" name="FW_C" display_name="FW_C" os="sun" user_name="" passwd="" baseline_profile="" log_collection_mode="extensive" host_name="10.10.10.3" log_server="JAPAN_Pry"/>
</CMA>
<CMA display_name="USA" ssh_port="" secondary_host_ip="" collector="Collector_B" communication="cpstat" Allow_Auto_Implementation="no" secondary_host_name="" secondary_host_sic_name="" monitoring="yes" audit_from_clm="no" log_collection_frequency="60" type="CMA" name="" host_name="" use_opsec_data_collection="yes" user_name="" passwd="" use_opsec_lea="yes" os="linux" defined="true">
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_D" name="FW_D" display_name="FW_D" os="sun" user_name="" passwd="" baseline_profile="" log_collection_mode="extensive" host_name="10.10.10.4" log_server="USA_Pry"/>
</CMA>
</FIREWALLS>
</CUSTOMER>

我需要得到&#34; display_name&#34;和&#34;收藏家&#34;来自CMA节点的值。然后,根据每个CMA节点,我需要获得关联的&#34; original_name&#34;和&#34; log_server&#34;每个FW_CKP子节点。最后,目的是建立一个表格,每个行的格式如下:[CMA NAME] - [COLLECTOR] - [ORIGINAL NAME] - [LOG SERVER NAME]。

这是我目前的代码:

' Load XML file
            doc.Load(tb_FilePath.Text)
'XML node path for CMA and FW_CKP
        CMA_Nodes = doc.SelectNodes("/CUSTOMER /FIREWALLS/CMA")
        FW_CKP_Nodes = doc.SelectNodes("/CUSTOMER /FIREWALLS/CMA/FW_CKP")

        'loop to go through each CAM nodes
        For Each CMA_Node As System.Xml.XmlElement In CMA_Nodes

            'operation inside the CMA balise
            CMA_Name = CMA_Node.Attributes(0).InnerText
            CMA_Collector = CMA_Node.Attributes(3).InnerText

            'loop to go through each FW_CKP nodes
            For Each FW_CKP_Node As System.Xml.XmlElement In FW_CKP_Nodes

                'Operation inside the FW_CKP baslise
                Original_Name = FW_CKP_Node.Attributes(3).InnerText
                Log_Server = FW_CKP_Node.Attributes(12).InnerText

                'update the table with the CMA name
                DataGridView1.Rows.Add(New String() {CMA_Name, CMA_Collector, Original_Name, Log_Server})

            Next

        Next

因此,代码将通过第一个CMA节点,然后不仅仅查看FW_CKP子节点,而是在XML文件上遍历所有这些节点并在表中添加附加行(错误的行)。

如何获得以下结果:

[CMA] - [收藏家] - [原创名称] - [日志服务器名称]

JAPAN - Collector_A - FW_A - JAPAN_PRY

JAPAN - Collector_A - FW_B - JAPAN_PRY

JAPAN - Collector_A - FW_C - JAPAN_PRY

USA - Collector_B - FW_D - USA_Pry

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您正在全局搜索FW_CKP节点,而不是每个CMA节点。

但是如果所有FW_CKP节点都是CMA节点的唯一直接子节点,则可以使用CMA_Node.ChildNodes:

   For Each CMA_Node As System.Xml.XmlElement In CMA_Nodes

        'operation inside the CMA balise
        CMA_Name = CMA_Node.Attributes(0).InnerText
        CMA_Collector = CMA_Node.Attributes(3).InnerText

        'loop to go through each FW_CKP nodes
        For Each FW_CKP_Node As System.Xml.XmlElement In CMA_Node.ChildNodes

            'Operation inside the FW_CKP baslise
            Original_Name = FW_CKP_Node.Attributes(3).InnerText
            Log_Server = FW_CKP_Node.Attributes(12).InnerText

            'update the table with the CMA name
            DataGridView1.Rows.Add(New String() {CMA_Name, CMA_Collector, Original_Name, Log_Server})

        Next

    Next