I'm new to c# I'm trying to load data from xml to combobox items with text and values my xml structure
<?xml version="1.0" encoding="UTF-8"?>
<roomsgg id="1">
<rooms id="1">
<roomtype id="1" name="standard">
<roomtimeprice id="5">
<rtid>5</rtid>
</roomtimeprice>
<roomtimeprice id="6">
<rtid>6</rtid>
</roomtimeprice>
</roomtype>
<roomtype id="2" name="sweet">
<roomtimeprice id="7">
<rtid>7</rtid>
</roomtimeprice>
<roomtimeprice id="8">
<rtid>8</rtid>
</roomtimeprice>
<roomtimeprice id="9">
<rtid>9</rtid>
</roomtimeprice>
</roomtype>
<roomtype id="3" name="gfgfgfgfgfgf">
<roomtimeprice id="10">
<rtid>10</rtid>
</roomtimeprice>
<roomtimeprice id="11">
<rtid>11</rtid>
</roomtimeprice>
<roomtimeprice id="12">
<rtid>12</rtid>
</roomtimeprice>
</roomtype>
</rooms>
</roomsgg>
I want to fill combobox with roomtype text and set the id as value for the item
public class ComboboxItem
{
public string Text { get; set; }
public string Value { get; set; }
public override string ToString()
{
return Text;
}
}
public frmChangeRoom(string hotel_id2)
{
InitializeComponent();
hotel_id3 = hotel_id2;
fillCombo();
}
//view data select type rooms
void fillCombo()
{
string mvalue = "1";
XDocument readrooms = XDocument.Load("http://www.website.com/api/api.php?action=getrooms&hotel_id=" + hotel_id3);
var mnodes = (from z in readrooms.Descendants("rooms").Where(e => e.Parent.Attribute("id").Value == mvalue)
select new
{
roomtypemainid = (string)z.Element("roomtype").Attribute("id").Value,
roomtypemainname = (string)z.Element("roomtype").Attribute("name").Value
}).ToList();
foreach (var z in mnodes)
{
ComboboxItem itemz = new ComboboxItem();
itemz.Text = z.roomtypemainname;
itemz.Value = z.roomtypemainid;
sel_typeRoom.Items.Add(itemz);
}
the problem is that only the first wanted element appears which is standard any idea?
solved :
XDocument readrooms = XDocument.Load("http://www.website.com/api/api.php?action=getrooms&hotel_id=" + hotel_id3);
readrooms.Descendants("roomtype").Select(p => new
{
roomtypename = p.Attribute("name").Value,
roomtypeid = p.Attribute("id").Value
}).ToList().ForEach(p =>
{
ComboboxItem itemz = new ComboboxItem();
itemz.Text = p.roomtypename;
itemz.Value = p.roomtypeid;
sel_typeRoom.Items.Add(itemz);
});
The problem is that you are selecting an element that you have only one of it: "rooms"
. The descendants method doesn't return a list of descendants of the element you gave but the list of descendant elements of that type.
Change your link like this: ( See that the select
already creates your custom type )
XDocument readrooms = XDocument.Load("http://www.wiseuo.com/api/api.php?action=getrooms&hotel_id=" + hotel_id3);
var mnodes = (from x in readrooms.Descendants("roomsgg")
where x.Attribute("Id").Value == mvalue
from z in x.Descendants("roomtype")
select new ComboboxItem
{
Value = (string)z.Attribute("id").Value,
Text = (string)z.Attribute("name").Value
}).ToList();
With adding your selection on the parent
element for checking the id
and then getting all the items it has under it it is like using the SelectMany
method in the method syntax
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.