Hi, extracting Specific XML Node
data from XML file and placing to DataTable is pretty simple, Find the below
XML file where specific Node I have taken is <TABLES>, From <TABLES> Each <TABLE> information
is collected to the DataTable.
XML: Myfile.xml
<BATCHEXTRACT>
<DBDETAILS>
<SQLCONNECTIONSTRING>server=dev.logs;database=DB;uid=admin;pwd=ast</SQLCONNECTIONSTRING>
<CONNECTIONTIMEOUT>2000</CONNECTIONTIMEOUT>
</DBDETAILS>
<LOGFILE>
<LOGFILEPATH>D:PURGE_BATCH/LogFile</LOGFILEPATH>
<LOGFILENAME>
BATCH_LogFile.log</LOGFILENAME>
</LOGFILE>
<TABLES>
<TABLE>
<Name>T_dATA_LOG</Name>
<NoOfDay>18</NoOfDay>
<dateColumn>CRT_DT</dateColumn>
</TABLE>
<TABLE>
<Name>T_INBOUND_LOG </Name>
<NoOfDay>18</NoOfDay>
<dateColumn>CRT_DT</dateColumn>
</TABLE>
<TABLE>
<Name>T_DELETE_LOG</Name>
<NoOfDay>18</NoOfDay>
<dateColumn>CRT_DT</dateColumn>
</TABLE>
<TABLE>
<Name>T_MERGE_LOG</Name>
<NoOfDay>28</NoOfDay>
<dateColumn>CRT_DT</dateColumn>
</TABLE>
<TABLE>
<Name>T_ALPHA_DETAILS</Name>
<NoOfDay>18</NoOfDay>
<dateColumn>CRT_DATE</dateColumn>
</TABLE>
</TABLES>
</BATCHEXTRACT>
C#
public DataTable GetTableNames()
{
try
{
XmlDocument Docs = new XmlDocument();
Docs.Load("Myfile.xml");
XmlElement root = Docs.DocumentElement;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("NoOfDay");
dt.Columns.Add("dateColumn");
XmlNodeList nodes = root.SelectNodes("//TABLES/TABLE");
foreach (XmlNode node in nodes)
{
dt.Rows.Add(node["Name"].InnerText.ToString(),
node["NoOfDay"].InnerText.ToString(),
node["dateColumn"].InnerText.ToString()
);
}
return dt;
}
catch (Exception
es)
{
throw es;
}
}