Converting RSS feed into DataTable



Hi i am reading the RSS feed and creating an XML using the DataTable. This is my code



try
{
DataTable tbl = new DataTable();
tbl.Columns.Add("id");
tbl.Columns.Add("product_name");
tbl.Columns.Add("description");

//Extra Nodes
tbl.Columns.Add("brand");
tbl.Columns.Add("condition");
tbl.Columns.Add("product_type");

XmlDocument doc = new XmlDocument();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(s);
XmlNodeList itemNodes = xmlDoc.SelectNodes("//rss/channel/item");
foreach (XmlNode itemNode in itemNodes)
{
DataRow row = tbl.NewRow();
XmlNode idNode = itemNode.SelectSingleNode("id");
XmlNode product_nameNode = itemNode.SelectSingleNode("product_name");
XmlNode descriptionNode = itemNode.SelectSingleNode("description");

//extra nodes
XmlNode brandNode = itemNode.SelectSingleNode("brand");
XmlNode conditionNode = itemNode.SelectSingleNode("condition");
XmlNode product_typeNode = itemNode.SelectSingleNode("product_type");



if (idNode != null && product_nameNode != null && descriptionNode != null && linkNode != null && image_linkNode != null && priceNode != null && availabilityNode != null)
{
row[0] = idNode.InnerText;
row[1] = product_nameNode.InnerText;
row[2] = descriptionNode.InnerText;
row[3] = linkNode.InnerText;

//extra nodes
if (brandNode == null)
row[4] = "";
else
row[4] = brandNode.InnerText;

if (conditionNode==null)
row[5] = "";
else
row[5] = conditionNode.InnerText;

if (product_typeNode==null)
row[6] = "";
else
row[6] = product_typeNode.InnerText;

}
tbl.Rows.Add(row);
// tbl.Rows.Add(row);
}

}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();

}


This is working fine without any issue but i want to make my code more efficient. Is this the good way to read the Rss and add into the datatable ?


No comments:

Post a Comment