.NET how to parse both parent and child XML node attributes



I'm trying to get specific attribute values from an XML file an then put them into a table.


Here is an extract of the XML file:



<?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>


I need to get "display_name" and "collector" values from CMA nodes. Then per each CMA nodes I need to get the associated "original_name" and "log_server" per FW_CKP child nodes. At the end, the purpose is to have a table where each rows are formated like that: [CMA NAME] - [COLLECTOR] - [ORIGINAL NAME] - [LOG SERVER NAME].


Here is my current code:



' 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


So the code is going through the first CMA nodes and then instead of ONLY looking at the FW_CKP child nodes, it is going through all of them on the XML file and putting addional rows in the table (wrong ones).


How can I get the below result:


[CMA] - [COLLECTOR] - [ORIGINAL NAME] - [LOG SERVER NAME]


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


Thank you for your support.


Cheers,


Alex


No comments:

Post a Comment