c#将xml加载到组合框中

时间:2016-09-06 09:08:32

标签: c# xml linq

我是c#的新手我试图将数据从xml加载到带有文本和值的组合框项目 我的xml结构

$ export DJANGO_SETTINGS_MODULE='project_name.settings'

我想用roomtype文本填充组合框并将id设置为项目的值

<?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>

问题是只出现第一个想要的元素,这是标准的 任何想法?

解决了:

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);
    }

1 个答案:

答案 0 :(得分:0)

问题是您选择的元素只有一个元素: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(); 已创建您的自定义类型

parent

id元素上添加您的选择以检查SelectMany,然后获取它下面的所有项目,就像在方法语法中使用 mixed score type 1 1 0.0183232 positive 2 neutral <NA> <NA> 3 -0.566558 negative <NA> 4 0.473484 positive <NA> 5 0.856743 positive <NA> 6 -0.422655 negative <NA> 方法一样