2014-09-26 181 views
0

我试图从XML文件中获取特定属性值,然后将它们放入表中。如何解析父节点和子节点的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> 

我需要得到“DISPLAY_NAME”,并从CMA节点“收藏家”的价值观。然后,每个CMA节点需要为每个FW_CKP子节点获取关联的“original_name”和“log_server”。最后,目的是建立一个表格,其中每行是如此形成的:[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] - [电器] - [原名] - [日志服务器名称]

日本 - Collector_A - FW_A - JAPAN_Pry

JAPAN - Collector_A - FW_B - JAPAN_Pry

日本 - Collector_A - FW_C - JAPAN_Pry

美国 - Collector_B - FW_D - USA_Pry

+0

我编辑了自己的冠军。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 – 2014-09-26 16:51:05

回答

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 
+0

它工作完美!非常感谢你! – SkorPPio 2014-09-27 19:29:51