I have a method that goes through a very large xml. It takes a while to process so I was thinking of applying Parallel programming into it to make it fast. I am however new to this parallel programming and have no idea yet how. Is the ff. method below applicable to parallel programming? Thanks in advance for your help.
public OpsCategoryList FilterAvailableItemCategory(string xmlCategory, string categoryID)
{
XmlDocument categoryXML = new XmlDocument();
categoryXML.LoadXml(xmlCategory);
OpsCategoryList ol = new OpsCategoryList();
try
{
foreach (XmlNode x in categoryXML.SelectSingleNode("//CategoryList").SelectNodes("Category[@SuperParentOID='" + categoryID+ "'][@IsLeafFlag='1'][@IsHiddenFlag='0']"))
{
OpsCategory b = new OpsCategory();
b.CategoryOID = x.Attributes["CategoryOID"].Value;
b.ParentOID = x.Attributes["ParentOID"].Value;
OpsItems oi = new OpsItems();
oi = LoadOpsItems(b.CategoryOID);
if (oi.Items == "")
{
x.Attributes["IsHiddenFlag"].Value = "1";
}
}
ol.categoryList = categoryXML.InnerXml;
}
catch (Exception ex)
{
ol.Error = "FilterAvailableItemCategory() web method failed on call to dbHelper.GetDataSet - " + ex.Message;
}
return ol;
}
this is the LoadOpsItems method
public OpsItems LoadOpsItems(string categoryOID)
{
//get ops config info
OpsConfig oc = new OpsConfig();
oc = GetOpsConfigInfo();
//get ops suppliers
DataSet ds = null;
Hashtable param = new Hashtable();
#region SQL Code
string sqlStr = @"exec DBO.uspOpsSupplierSelect @InternalEntityID='" + base.PmcID + "', @InternalUserID='" + base.UserID +
"', @InternalSiteID='" + base.SiteID + "', @IVPC_Name='', @IVPC_Code=''";
#endregion
OpsItems oi = new OpsItems();
OpsSuppliers ap = new OpsSuppliers();
XmlDocument suppliers = new XmlDocument();
string tempSupplierXML = "<root>";
XmlDocument items = new XmlDocument();
string returnXML = "";
try
{
dbHelper.Entity = xSiteDB.DBEntity.Site;
ds = dbHelper.GetDataSet(sqlStr, param, "Select");
Array.Resize(ref ap.Sup, ds.Tables[0].Rows.Count);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
#region Field assignment
Suppliers tr = new Suppliers();
tempSupplierXML += "<Row sosID='" + ds.Tables[0].Rows[i]["sosID"].ToString() + "' ";
tempSupplierXML += "sosSupplierOID='" + ds.Tables[0].Rows[i]["sosSupplierOID"].ToString() + "' ";
tempSupplierXML += "sosSupplierName='" + ds.Tables[0].Rows[i]["sosSupplierName"].ToString() + "' ";
tempSupplierXML += "sosSupplierCode='" + ds.Tables[0].Rows[i]["sosSupplierCode"].ToString() + "' ";
tempSupplierXML += "Cnt='" + ds.Tables[0].Rows[i]["Cnt"].ToString() + "' />";
#endregion
}
tempSupplierXML += "</root>";
suppliers.LoadXml(tempSupplierXML);
#region build http request
string XML = "<Mxml Version='1.0'>" +
"<TradingPartner>" +
"<PartnerName>" + oc.socPartnerName + "</PartnerName>" +
"<Identity>" + oc.socIdentity + "</Identity>" +
"</TradingPartner>" +
"<PurchaseGroup>" +
"<GroupName></GroupName>" +
"<GroupCode>" + oc.socGroupCode + "</GroupCode>" +
"<Address>" +
"<Attention1/>" +
"<Attention2/>" +
"<Address1></Address1>" +
"<Address2></Address2>" +
"<Address3/>" +
"<City></City>" +
"<State></State>" +
"<PostalCode></PostalCode>" +
"<Country></Country>" +
"<PhoneNumber/>" +
"<FaxNumber/>" +
"<EmailAddress/>" +
"</Address>" +
"</PurchaseGroup>" +
"<Request>" +
"<GetCategoryProductRequest>" +
"<CompanyCode>" + oc.socCompanyCode + "</CompanyCode>" +
"<PropertyCode></PropertyCode>" +
"<SupplierCodeList>";
foreach (XmlNode x2 in suppliers.SelectSingleNode("root").SelectNodes("Row"))
{
XML += "<SupplierCode>" + x2.Attributes["sosSupplierCode"].Value + "</SupplierCode>";
}
XML += "</SupplierCodeList>" +
"<CategoryOID>" + categoryOID + "</CategoryOID>" +
"<PageSize> 999 </PageSize>" +
"<PageNumber> 1 </PageNumber>" +
"</GetCategoryProductRequest>" +
"</Request>" +
"</Mxml>";
#endregion
#region pass xml to ops
WebRequest wr = WebRequest.Create(oc.socOpsURL);
wr.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(XML);
wr.ContentType = "application/x-www-form-urlencoded";
wr.ContentLength = byteArray.Length;
Stream wrRequest = wr.GetRequestStream();
wrRequest.Write(byteArray, 0, byteArray.Length);
wrRequest.Close();
Stream wrResponse = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(wrResponse);
string postResponse = sr.ReadToEnd();
sr.Close();
wrResponse.Close();
#endregion
items.LoadXml(postResponse);
returnXML += items.SelectSingleNode("//ProductList").InnerXml;
oi.Items = returnXML;
}
catch (Exception ex)
{
oi.Error = "LoadOpsItems() web method failed on call to dbHelper.GetDataSet - " + ex.Message;
}
return oi;
}