Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic xpath in c# code

Newbie
 
Join Date: Jun 2007
Posts: 2
#1: Jun 23 '07
Hello!

I want to generate dynamicaly xpath in c#.net code from xml file which is having multiple child elements under parent element.The purpose of generating xpath is uses for calculation.The following is the code is hard coded mentioning particular nodes for calculating commission.
public void CalculateCommissionAmount(string policyXML, string rate)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(policyXML);
XmlElement root = xmldoc.DocumentElement;

if (Convert.ToDouble(rate) < -5)
{
MessageBox.Show("Invalid Rate - Rate cannot be less than -5%.");
}
else if (Convert.ToDouble(rate) > 100)
{
MessageBox.Show("Invalid Rate - Rate cannot be more than 100%.");
}
else
{
try
{
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("PersonalAutoVehicle") ;
for(int i=0; i<xmlnode.Count; i++)
{
cmmAmount = Convert.ToDouble(rate)/100 * Convert.ToDouble(xmlnode[i].SelectSingleNode("PersonalAutoVehicleLiabilityCov erage/BasePremium").InnerText);
xmlnode[i].SelectSingleNode("PersonalAutoVehicleLiabilityCov erage/FFICCombinedSingleLimitCommissionAmount").InnerTex t = Convert.ToString(cmmAmount);
cmmTotalAmount += cmmAmount;
}

XmlNode node = root.SelectSingleNode("Policy/PersonalAuto/CommissionRate");
node.InnerText = rate;

XmlNode amtNode = root.SelectSingleNode("Policy/PersonalAuto/FFICTotalCommissionAmountForVehicleLiabilityCovera ge");
amtNode.InnerText = Convert.ToString(cmmTotalAmount);

FileStream fsxml = new FileStream(policyXML,FileMode.Truncate,FileAccess. Write,FileShare.ReadWrite);
xmldoc.Save(fsxml);
}
catch(Exception ex)
{
WriteToEventLog(ex.Message, "CommissionCalculation", EventLogEntryType.Error, "FFICCommission");
MessageBox.Show("Error logged.");
}
}
}

i want to calculate for multiple checking multiple node,so please help me how to generate dynamicaly xpath.

thank you

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Jun 23 '07

re: Dynamic xpath in c# code


Quote:

Originally Posted by surya926

Hello!

I want to generate dynamicaly xpath in c#.net code from xml file which is having multiple child elements under parent element.The purpose of generating xpath is uses for calculation.The following is the code is hard coded mentioning particular nodes for calculating commission.
public void CalculateCommissionAmount(string policyXML, string rate)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(policyXML);
XmlElement root = xmldoc.DocumentElement;

if (Convert.ToDouble(rate) < -5)
{
MessageBox.Show("Invalid Rate - Rate cannot be less than -5%.");
}
else if (Convert.ToDouble(rate) > 100)
{
MessageBox.Show("Invalid Rate - Rate cannot be more than 100%.");
}
else
{
try
{
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("PersonalAutoVehicle") ;
for(int i=0; i<xmlnode.Count; i++)
{
cmmAmount = Convert.ToDouble(rate)/100 * Convert.ToDouble(xmlnode[i].SelectSingleNode("PersonalAutoVehicleLiabilityCov erage/BasePremium").InnerText);
xmlnode[i].SelectSingleNode("PersonalAutoVehicleLiabilityCov erage/FFICCombinedSingleLimitCommissionAmount").InnerTex t = Convert.ToString(cmmAmount);
cmmTotalAmount += cmmAmount;
}

XmlNode node = root.SelectSingleNode("Policy/PersonalAuto/CommissionRate");
node.InnerText = rate;

XmlNode amtNode = root.SelectSingleNode("Policy/PersonalAuto/FFICTotalCommissionAmountForVehicleLiabilityCovera ge");
amtNode.InnerText = Convert.ToString(cmmTotalAmount);

FileStream fsxml = new FileStream(policyXML,FileMode.Truncate,FileAccess. Write,FileShare.ReadWrite);
xmldoc.Save(fsxml);
}
catch(Exception ex)
{
WriteToEventLog(ex.Message, "CommissionCalculation", EventLogEntryType.Error, "FFICCommission");
MessageBox.Show("Error logged.");
}
}
}

i want to calculate for multiple checking multiple node,so please help me how to generate dynamicaly xpath.

thank you

Let me ask around, surya926!

Will let you know, as I do no know C# quite yet.

Please stay tuned!

Dököll
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#3: Jun 25 '07

re: Dynamic xpath in c# code


I have moved this thread to the .NET forum, as the experts there have far more experience with C#.
Reply