简体   繁体   English

C#将XML加载到组合框

[英]c# load xml into combobox

I'm new to c# I'm trying to load data from xml to combobox items with text and values my xml structure 我是C#的新手,正在尝试将xml结构中的数据从xml加载到带有文本和值的组合框项目

<?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 我想用房型文本填充组合框并将ID设置为项目的值

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" . 问题是您选择的元素只有一个: "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 ) 像这样更改您的链接:( 请参见select已创建您的自定义类型

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 通过在parent元素上添加选择以检查id ,然后获取其下的所有项,就像在方法语法中使用SelectMany方法一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM