Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi i have a problem, i want to get my xmlnodelist inside my xmlnode foreach somehowe because i have counter on it, an i need to get the of addon attribute match the id of textbox and nodes.
My final goal is a shooping basket. Where i count the number in the textbox and counting, it with the price that comes from an xmlnode called AddonPrice.
Right now i only get the first addon not the two others, so it looks like this right now.

Type:Ticket counts:1 price:178 total price: 178

and the counting works fine, but only with the first node. I think my problem is this line doc.SelectNodes("TourInfo/AddOns/AddOn[@Id='" + id + "']");. Because its outside my foreach so the id count will allways be 0. So i need help to get it inside my foreach or, do it some other way :)

C#
string id = "addon" + count;
        string addon;
                
        XmlNodeList nodeListADDon = doc.SelectNodes("TourInfo/AddOns/AddOn[@Id='" + id + "']");
        litTotal.Text = "";
        litAdd.Text = "";
        foreach (XmlNode nodeA in nodeListADDon)
        {
         
            addon = Request.Form["txtAddonAntal" + count];
            antal = int.Parse(addon.ToString());

            pris = int.Parse(nodeA["AddonPrice"].InnerText);
            ansver += antal * pris;
           total += ansver;

            litTotal.Text += total.ToString() + " kr";
            litAdd.Text = "<table class="table table-condensed"><tr>" + "<td>" + nodeA["AddonText"].InnerText + "</td>" + "<td>" + antal.ToString() + "</td>" + "<td>Pris:&nbsp;" + ansver.ToString() + "&nbsp;kr</td>" + "</tr></table><br />";
            count++;
        }



EDIT
XML
<?xml version="1.0" standalone="no"?>
<!-- 0031114_reply.xml - generated by ACUCOBOL-GT v9.2.3 + ECN-4310 + ECN-4314 + ECN-4316 on 2015/06/25 -->
<TourInfo>
  <Recordtype>TourInfo</Recordtype>
  <Trip>1609</Trip>
  <TransportForm>2</TransportForm>
  <Accommodation>200</Accommodation>
  <AvailableTrips>14</AvailableTrips>
  <AddOns>
    <AddOn Id="addon0">
      <AddonCode>1</AddonCode>
      <AddonText>Barnestol</AddonText>
      <AddonAccNo>200</AddonAccNo>
      <AddonPrice>150</AddonPrice>
      <AddonPriceOffersText>Tilbud:</AddonPriceOffersText>
      <AddonPriceOffers>125</AddonPriceOffers>
    </AddOn>
    <AddOn Id="addon1">
      <AddonCode>2</AddonCode>
      <AddonText>Tremmeseng</AddonText>
      <AddonAccNo>200</AddonAccNo>
      <AddonPrice>200</AddonPrice>
      <AddonPriceOffersText />
      <AddonPriceOffers />
    </AddOn>
    <AddOn Id="addon2">
      <AddonCode>9</AddonCode>
      <AddonText>Billet</AddonText>
      <AddonAccNo>67</AddonAccNo>
      <AddonPrice>178</AddonPrice>
      <AddonPriceOffersText />
      <AddonPriceOffers />
    </AddOn>
  </AddOns>
Posted
Updated 24-Jun-15 23:44pm
v4
Comments
Andy Lanng 24-Jun-15 4:21am    
I don't get what you're trying to do. Please use the "Improve Question" button above this comment and go into a bit more detail.
Please include your ultimate goal, your current intention (which is the bit you have included) and why you cannot achieve your stated goal.

Thanks ^_^
Andy Lanng 24-Jun-15 4:41am    
Great job. Much better ^_^
I remember this one :D

1 solution

Ok - I see:

You are correct. The list nodelist is only iterated once for 1 id. Try wrapping this in a for loop:

if the id is being iterated


C#
for(int x= 0;x<doc.SelectNodes("TourInfo/AddOns/AddOn").Count;x++)
{

        string addon;
                
        XmlNodeList nodeListADDon = doc.SelectNodes("TourInfo/AddOns/AddOn[@Id='addon" + x + "']");
        litTotal.Text = "";
        litAdd.Text = "";
        foreach (XmlNode nodeA in nodeListADDon)
        {
         
            addon = Request.Form["txtAddonAntal" + x];
            antal = int.Parse(addon.ToString());
 
            pris = int.Parse(nodeA["AddonPrice"].InnerText);
            ansver += antal * pris;
           total += ansver;
 
            //Formatted for clarity
            litTotal.Text += total.ToString() + "&nbsp;kr";
            string text = "<table class="table table-condensed"><tr>" 
            text += "<td>" + nodeA["AddonText"].InnerText + "</td>" 
            text += "<td>" + antal.ToString() + "</td>" 
            text += "<td>Pris:&nbsp;" + ansver.ToString() 
            text += "&nbsp;kr</td>" 
            text += "</tr></table><br />";

            litAdd.Text = text;

        }
}




UPDATE: I now have access to the OP source and code:


C#
private static void testsss()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\3rd Party\testsss.xml");

    int total = 0;

    litTotal.Text = "";
    litAdd.Text = "";

    //Error handling
    var allAddonNodes = doc.SelectNodes("TourInfo/AddOns/AddOn");
    if (allAddonNodes == null)
        return;

    for (int x = 0; x < allAddonNodes.Count; x++)
    {
        string textBoxname = string.Format("txtAddonAntal{0}", x);

        XmlNode addonNode = allAddonNodes[x];
        //Error handling
        if (addonNode["AddonPrice"] == null)
            continue;

        //Error handling
        int antal;
        if (!int.TryParse(Request.Form[textBoxname], out antal))
            continue;

        //Error handling
        int pris;
        if (!int.TryParse(addonNode["AddonPrice"].InnerText, out pris))
            continue;

        var ansver = antal * pris;
        total += ansver;


        var nodeAAddonText = addonNode["AddonText"];

        string nodeAInnerText = nodeAAddonText == null ? "" : nodeAAddonText.InnerText;

        //This line seems out of place
        litAdd.Text = string.Format(FormatText, nodeAInnerText, antal, ansver);

    }
    litTotal.Text = total + @"&nbsp;kr";
}




FINAL UPDATE: Some ideas about litAdd:

private static void testsss()
{
    //If you have a table included in the markup then run that at server
    //and use that instead.  The method below is a bit of a hack.

    //We'll use this to write the table markup to a string.  This is the hack part
    StringBuilder sb = new StringBuilder();
    HtmlTextWriter writer = new HtmlTextWriter( new StringWriter(sb,CultureInfo.InvariantCulture));

    //table to use in the hack
    HtmlTable table = new HtmlTable();
    table.Attributes.Add("class", "table table-condensed");



    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\3rd Party\testsss.xml");

    int total = 0;

    litTotal.Text = "";
    litAdd.Text = "";

    //Error handling
    var allAddonNodes = doc.SelectNodes("TourInfo/AddOns/AddOn");
    if (allAddonNodes == null)
        return;

    for (int x = 0; x < allAddonNodes.Count; x++)
    {
        //create a new table row
        HtmlTableRow row = new HtmlTableRow();
        // a cell instance to add to the row.
        HtmlTableCell cell;

        string textBoxname = string.Format("txtAddonAntal{0}", x);

        XmlNode addonNode = allAddonNodes[x];
        //Error handling
        if (addonNode["AddonPrice"] == null)
            continue;

        //Error handling
        int antal;
        if (!int.TryParse(RequestForm[textBoxname], out antal))
            continue;

        //Error handling
        int pris;
        if (!int.TryParse(addonNode["AddonPrice"].InnerText, out pris))
            continue;

        var ansver = antal * pris;
        total += ansver;


        var nodeAAddonText = addonNode["AddonText"];

        string nodeAInnerText = nodeAAddonText == null ? "" : nodeAAddonText.InnerText;

        cell = new HtmlTableCell();
        cell.InnerText = nodeAInnerText;
        row.Cells.Add(cell);

        cell = new HtmlTableCell();
        cell.InnerText = antal.ToString();
        row.Cells.Add(cell);

        cell = new HtmlTableCell();
        cell.InnerText = string.Format("Pris:&nbsp;{0}&nbsp;kr", ansver);
        row.Cells.Add(cell);

        //add the row to the table
        table.Rows.Add(row);

    }

    //if the table exists already, then the page render will draw it, the rows and the cells
    //when the page is rendered.  In this hack it is not in the page so we have to render it
    //ourselves and get the html.  This is a hack (but a good one ;)

    table.RenderControl(writer);

    litAdd.Text = sb.ToString();

    litTotal.Text = total + @"&nbsp;kr";
}
 
Share this answer
 
v8
Comments
Martin Lauritsen 24-Jun-15 5:04am    
thx m8 i only get one last thing, before i can try it out.
This line for(int x= 0;x<("TourInfo/AddOns/AddOn").Count();x++) my problem is "Count" it says that string does not contain a defintion for 'Count' and no extension method of count exists " in Visuel studio". I have try to wrap it in an string and int
Andy Lanng 24-Jun-15 5:41am    
It's just an example. int x has replaced int count. Did that have a max value? If so then replace that line with for(int x = countstart;x<countmax;x++)
Martin Lauritsen 25-Jun-15 4:19am    
hey no i doesnt have a max value, my line looks like this
for(int count = 0;count<("TourInfo/AddOns/AddOn").Count();count++), but im not sure what i should do with this .Count() :)
Andy Lanng 25-Jun-15 4:27am    
oh right. Count is a property, not a method:
or(int count = 0;count<("TourInfo/AddOns/AddOn").Count;count++)
Note that the brackets have been removed

This will work ^_^
Martin Lauritsen 25-Jun-15 4:30am    
hmm nope it still says, that string does not contain a definition and no extensin method for count. Should i define it in a string, or create a method? :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900