Click here to Skip to main content
15,890,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem with my xml file, I have an xml file consisting of bikes. And for each bike type, there are different sizes. And I will then make two dropdown menus. Where one is bicycles and in the next are the sizes available for that bike.
It works fine to read the bike types, but when I try to read the sizes that fit the bike, it reads all sizes into the dropdown, rather than those that fit the bike. I have added my C # and xml file

DropDownList ddl = new DropDownList();
ddl.CssClass = "";
ddl.ID = "ddlBike" + i;
ddl.Items.Add(new ListItem("Vælg cykel", "0"));


DropDownList ddlSize = new DropDownList();
ddlSize.CssClass = "";
ddlSize.ID = "ddlSize" + i;

ddlSize.Items.Add(new ListItem("Højde", "0"));





XmlNodeList nodeList = doc.SelectNodes("InfoBikeResponse/BikeTypes/BikeType");

foreach (XmlNode node in nodeList)
{

    ddl.Items.Add(new ListItem(node["Id"].InnerText + HttpUtility.HtmlDecode("  ") + "" + HttpUtility.HtmlDecode("  ") + node["TypeName"].InnerText + ""));
    ddl.ClearSelection();

    XmlNodeList nodeListSize = node.SelectNodes("Sizes/Size");


    foreach (XmlNode nodeSize in nodeListSize)
    {
        ddlSize.Items.Add(new ListItem(nodeSize["SizeId"].InnerText + HttpUtility.HtmlDecode("  ") + "" + HttpUtility.HtmlDecode("  ") + nodeSize["SizeName"].InnerText, nodeSize["SizeCount"].InnerText + ""));
        ddlSize.ClearSelection();

    }

}




<InfoBikeResponse>
  <BikeTypes>
    <BikeType>
      <Id>1</Id>
      <TypeName>Classic</TypeName>
      <TypeCount/>
      <TypePrice>150,00</TypePrice>
      <TypePriceVat>187,50</TypePriceVat>
      <Sizes>
        <Size>
          <SizeId>10</SizeId>
          <SizeName>155-167</SizeName>
          <SizeCount>2</SizeCount>
        </Size>
        <Size>
          <SizeId>11</SizeId>
          <SizeName>168-172</SizeName>
          <SizeCount>1</SizeCount>
        </Size>
        <Size>
          <SizeId>12</SizeId>
          <SizeName>173-185</SizeName>
          <SizeCount>1</SizeCount>
        </Size>
      </Sizes>
    </BikeType>
    <BikeType>
      <Id>2</Id>
      <TypeName>Sport Bike</TypeName>
      <TypeCount/>
      <TypePrice>185,00</TypePrice>
      <TypePriceVat>231,25</TypePriceVat>
      <Sizes>
        <Size>
          <SizeId>20</SizeId>
          <SizeName>155-167</SizeName>
          <SizeCount>2</SizeCount>
        </Size>
        <Size>
          <SizeId>21</SizeId>
          <SizeName>168-172</SizeName>
          <SizeCount>3</SizeCount>
        </Size>
      </Sizes>
    </BikeType>
    <BikeType>
      <Id>3</Id>
      <TypeName>MTB</TypeName>
      <TypeCount/>
      <TypePrice>220,00</TypePrice>
      <TypePriceVat>275,00</TypePriceVat>
      <Sizes>
        <Size>
          <SizeId>40</SizeId>
          <SizeName>155-167</SizeName>
          <SizeCount>1</SizeCount>
        </Size>
        <Size>
          <SizeId>41</SizeId>
          <SizeName>168-172</SizeName>
          <SizeCount>1</SizeCount>
        </Size>
      </Sizes>
    </BikeType>
    <BikeType>
      <Id>4</Id>
      <TypeName>Classic E</TypeName>
      <TypeCount/>
      <TypePrice>170,00</TypePrice>
      <TypePriceVat>212,50</TypePriceVat>
      <Sizes>
        <Size>
          <SizeId>51</SizeId>
          <SizeName>168-172</SizeName>
          <SizeCount>2</SizeCount>
        </Size>
      </Sizes>
    </BikeType>
  </BikeTypes>


What I have tried:

Linq, Xpath and Different kind of Google Solutions
Posted
Updated 23-Feb-18 22:06pm
Comments
j snooze 23-Feb-18 17:23pm    
You can't fill the second drop down without knowing what bike they selected. You need to fill the second drop down based off of the bikes selected index change or something likethat. Currently your query is telling the xml to give you all the Size element tags for every bike. First query the bike nodes (use SelectSingleNode where the id tag is whatever number they selected lookup xpath expressions for exact syntax) then once you have just the bike node they selected, you can use an xpath query on that node to query the sizes inside that node. Is that what you are looking for? This is a multiple step process, if I understand what you are trying to do.
Martin Lauritsen 23-Feb-18 17:36pm    
Provides just a little more explanation, I make an HTML table on the fly based on a number in a text box. As I send to a server, I also receive the xml you see. So I would like if it was possible to fill the second dropdown when it is created. I upload image, on that picture you can see the rows that I have made on the fly.
Martin Lauritsen 23-Feb-18 17:39pm    
http://i64.tinypic.com/2qv8ei1.png
RickZeeland 24-Feb-18 3:46am    
Is your question about ASP.NET ?

1 solution

I'd suggest to use XDocument class[^]. See:

string biketype = "Classic";

XDocument xdoc = XDocument.Load(<xml_stream_here>);
var bikeSizes = xdoc.Descendants("BikeType")
	.Where(x=>x.Element("TypeName").Value == biketype)
	.SelectMany(x=>x.Descendants("SizeName").Select(y=>y.Value))
	.ToList();

foreach(string s in bikeSizes)
{
	Console.WriteLine("{0}", s);
}


For further details, please see:
LINQ to XML (C#) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Martin Lauritsen 24-Feb-18 11:36am    
Thank you very much, the problem is now that it adds it to all my dropdowns. I'm doing dynamic controls creation, so if I've made 7 rows, it puts it into all of them. So i need to find the specific row index where classic has been selected. But thank you, I've been pointed in the right direction :)
Maciej Los 24-Feb-18 15:09pm    
You're very welcome.
If my answer was helpful, please, mark it as an answer (green button).
Martin Lauritsen 24-Feb-18 15:19pm    
The problem is no entirely fixed :) but will do

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