Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:26:08+00:00 2026-06-13T14:26:08+00:00

I am trying to write a program that will receive an input from a

  • 0

I am trying to write a program that will receive an input from a web service and select the correct information that goes along with the order ID. I am using xPath for this as it is required. For example, if I enter a 1 under the OrderID when I run the program it should pull the totalcost for that order and display it. I know there is definitely something wrong with my code, since I am new to this I have no idea how to physically grab an input when I have no control of the variables being used. Any help would be much appreciated, thanks!

Here is my code:

[WebMethod]
public int GetTotalCostForAnOrder(int OrderID)
{
    XPathNavigator nav;
    XPathDocument docNav;
    XPathNodeIterator NodeIter;

    String rootPath = Server.MapPath("~");
    string strFilename = rootPath + "\\OrderInfoLab3.xml";
    docNav = new XPathDocument(strFilename);

    // Create a navigator to query with XPath.
    nav = docNav.CreateNavigator();
    String searchString = "sum(OrderFeed/Order/Items/Item/TotalCost)";

    // you need to determine the proper XPath statement

    // Select the node and place the results in an iterator.
    NodeIter = nav.Select(searchString);

    while (NodeIter.MoveNext())
    {
        NodeIter.Current.Select("OrderID");
    }

    var totalOrder = nav.Compile(searchString);
    return Convert.ToInt32(nav.Evaluate(totalOrder));
}

Here is my XML File:

<Order id="1">
<BillingInformation>
    <Name>Bruce Ganek</Name>
    <Address>99 Main Street</Address>
    <City>Cranston</City>
    <State>RI</State>
    <ZipCode>02910</ZipCode>
</BillingInformation>
<ShippingInformation>
    <Name>Governor Chafee</Name>
    <Address>82 Smith St # 115</Address>
    <City>Providence</City>
    <State>RI</State>
    <ZipCode>02903-1121</ZipCode>
</ShippingInformation>
<Items>
    <Item>
        <PartNo>JETSWEATER</PartNo>
        <Description>N.Y. Jets Sweatshirt</Description>
        <UnitPrice>10.50</UnitPrice>
        <Quantity>2</Quantity>
        <TotalCost>21.00</TotalCost>
        <CustomerOptions>
            <Size>M</Size>
            <Color>Green</Color>
        </CustomerOptions>
    </Item>
    <Item>
        <PartNo>JETSWEATER</PartNo>
        <Description>N.Y. Jets Sweatshirt</Description>
        <UnitPrice>7.50</UnitPrice>
        <Quantity>3</Quantity>
        <TotalCost>22.50</TotalCost>

        <CustomerOptions>
            <Size>S</Size>
            <Color>White</Color>
        </CustomerOptions>
    </Item>
    <Item>
        <PartNo>JETSFLASHLIGHT</PartNo>
        <Description>N.Y. Jets Flashlight</Description>
        <UnitPrice>5.00</UnitPrice>
        <Quantity>1</Quantity>
        <TotalCost>5.00</TotalCost>

        <CustomerOptions/>

    </Item>

</Items>
</Order>

Here is the error I get in my output:

System.Xml.XPath.XPathException: Expression must evaluate to a node-set.
at System.Xml.XPath.XPathNavigator.Select(XPathExpression expr)
at System.Xml.XPath.XPathNavigator.Select(String xpath)
at Lab3.Service1.GetTotalCostForAnOrder(Int32 OrderID) in C:\
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:26:09+00:00Added an answer on June 13, 2026 at 2:26 pm

    You should use LINQ2XML

    public decimal GetTotalCostForAnOrder(int OrderID)
    {
        XElement doc=XElement.Load("c:\\yourXML.xml");
        decimal totalPrice=doc.DescendantsAndSelf("Order")
        .Where(x=>x.Attribute("id").Value==OrderID)
        .Select(y=>y.Element("Items")).Elements("Item")
        .Select(z=>decimal.Parse(z.Element("TotalCost").Value)).ToList().Sum();
        return totalPrice;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program that will receive a string of max
I'm trying to write a program that will read from a flat file of
Hey, I'm trying to write a program that will accept new tasks from people,
I'm trying to write a program that will read characters from stdin into a
I'm trying to write a program that will validate a user input consisting of
I am trying to write a program that will take the users input value
I am trying to write a program that will accept user input which will
I'm trying to write a program that will fetch data from a SQL Server
I am trying to write a simple Ruby program that I will run from
I am trying to write a program that will prompt you to enter someone's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.