473,770 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic xpath in c# code

2 New Member
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 CalculateCommis sionAmount(stri ng policyXML, string rate)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(pol icyXML);
XmlElement root = xmldoc.Document Element;

if (Convert.ToDoub le(rate) < -5)
{
MessageBox.Show ("Invalid Rate - Rate cannot be less than -5%.");
}
else if (Convert.ToDoub le(rate) > 100)
{
MessageBox.Show ("Invalid Rate - Rate cannot be more than 100%.");
}
else
{
try
{
XmlNodeList xmlnode = xmldoc.GetEleme ntsByTagName("P ersonalAutoVehi cle");
for(int i=0; i<xmlnode.Count ; i++)
{
cmmAmount = Convert.ToDoubl e(rate)/100 * Convert.ToDoubl e(xmlnode[i].SelectSingleNo de("PersonalAut oVehicleLiabili tyCoverage/BasePremium").I nnerText);
xmlnode[i].SelectSingleNo de("PersonalAut oVehicleLiabili tyCoverage/FFICCombinedSin gleLimitCommiss ionAmount").Inn erText = Convert.ToStrin g(cmmAmount);
cmmTotalAmount += cmmAmount;
}

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

XmlNode amtNode = root.SelectSing leNode("Policy/PersonalAuto/FFICTotalCommis sionAmountForVe hicleLiabilityC overage");
amtNode.InnerTe xt = Convert.ToStrin g(cmmTotalAmoun t);

FileStream fsxml = new FileStream(poli cyXML,FileMode. Truncate,FileAc cess.Write,File Share.ReadWrite );
xmldoc.Save(fsx ml);
}
catch(Exception ex)
{
WriteToEventLog (ex.Message, "CommissionCalc ulation", EventLogEntryTy pe.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
Jun 23 '07 #1
2 2701
Dököll
2,364 Recognized Expert Top Contributor
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 CalculateCommis sionAmount(stri ng policyXML, string rate)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(pol icyXML);
XmlElement root = xmldoc.Document Element;

if (Convert.ToDoub le(rate) < -5)
{
MessageBox.Show ("Invalid Rate - Rate cannot be less than -5%.");
}
else if (Convert.ToDoub le(rate) > 100)
{
MessageBox.Show ("Invalid Rate - Rate cannot be more than 100%.");
}
else
{
try
{
XmlNodeList xmlnode = xmldoc.GetEleme ntsByTagName("P ersonalAutoVehi cle");
for(int i=0; i<xmlnode.Count ; i++)
{
cmmAmount = Convert.ToDoubl e(rate)/100 * Convert.ToDoubl e(xmlnode[i].SelectSingleNo de("PersonalAut oVehicleLiabili tyCoverage/BasePremium").I nnerText);
xmlnode[i].SelectSingleNo de("PersonalAut oVehicleLiabili tyCoverage/FFICCombinedSin gleLimitCommiss ionAmount").Inn erText = Convert.ToStrin g(cmmAmount);
cmmTotalAmount += cmmAmount;
}

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

XmlNode amtNode = root.SelectSing leNode("Policy/PersonalAuto/FFICTotalCommis sionAmountForVe hicleLiabilityC overage");
amtNode.InnerTe xt = Convert.ToStrin g(cmmTotalAmoun t);

FileStream fsxml = new FileStream(poli cyXML,FileMode. Truncate,FileAc cess.Write,File Share.ReadWrite );
xmldoc.Save(fsx ml);
}
catch(Exception ex)
{
WriteToEventLog (ex.Message, "CommissionCalc ulation", EventLogEntryTy pe.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
Jun 23 '07 #2
Motoma
3,237 Recognized Expert Specialist
I have moved this thread to the .NET forum, as the experts there have far more experience with C#.
Jun 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
6825
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
2
1338
by: listerofsmeg | last post by:
Newbie here. I want to use a .config file with my simple console app, but no matter what I do I cannot get the "dynamic" rollout in my properties window. In the end I gave up and coded the calls to ConfigurationSettings.AppSettings.Get() myself, but my problem now is creating the config file. It looks like I'll have to do that manually too.
1
3098
by: David Laub | last post by:
I have no problems running the following dynamic XPath evaluator form MSXSL: <msxsl:script implements-prefix="dyn" language="jscript"> evaluate(context, expression) { return context.nextNode().selectNodes(expression);
0
1482
by: Thor W Hammer | last post by:
Is it possible to use dynamic xpath expressions with the <%#XPath("..")%> syntax and XmlDataSource? This is a couple of examples to illustrate what I think about: 1: <%#XPath("*")%> 2: <%#XPath("*%>]")%>
6
4939
by: dotnetnoob | last post by:
i would like to know how i can build xpath expression dynamiclly. let's say i have a following xml file: <EventEnrollment InstanceNumber = "675"> <EventSource> <ObjectReference ObjectKey="xxxxx"> .. .. .. <EventEnrollment InstanceNumber = "676">
0
3985
by: jeoffh | last post by:
Background: I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML column in a table and I would like to update/delete some values while leaving the other values alone. I am designing this database/table/column so maybe I could use attributes or elements/nodes, the choice is ultimately mine. The one constraint is that I have to allow for customized name/value pairs....
1
1809
by: =?Utf-8?B?dHV0b3I=?= | last post by:
hi folks i want complete control of my menuing in ASP (no site maps, no hardwired XML files, etc.) This is all governed by SiteID, ApplicationID, UserID, etc. i thought i was close with this, but nothing is rending in the control/on the page (even though I see my 12 items in the databindings are there menuName.databindings.item(0) - (11)). (Personally, I don't understand why this is so difficult) Here's my code:
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.