2015-01-26 44 views
0

简单的LINQ查询正在避开我。我已经做了很多谷歌,无济于事。从XML中填充多个DropDownLists(LINQ)

我的XML文件:

<?xml version="1.0" encoding="utf-8" ?> 
<Clients> 
    <Client name="TestClient"> 
    <cat ID="1" value="Computers"/> 
    <cat ID="2" value="Ayyy"/> 
    <cat ID="3" value="lmao"/> 
    </Client> 

    <Client name="DoTheNeedful"> 
    <cat ID="1">ارومیه </cat> 
    <cat ID="2">اشنویه </cat> 
    <cat ID="3">بوکان </cat> 
    </Client> 
</Clients> 

在我的视图模型,有一个变量声明为:

public IEnumerable<SelectListItem> Category { get; set; } 

我的LINQ查询:

 string path = Server.MapPath("~/Controllers/ClientConfig.xml"); 
     string whichDD = "TestClient"; 

     var model = new TicketViewModel 
     { 

      Category = 
      from dropDown in XDocument.Load(path).Descendants("Client") 
      where dropDown.Attribute("name").Value == whichDD 
      from name in dropDown.Descendants("name")         
      select new SelectListItem 
      { 
       Value = name.Attribute("ID").Value, 
       Text = name.Attribute("value").Value 
      } 
     }; 

我的观点:

@model IEGTicketingSite.Models.TicketViewModel 

@{ 
    ViewBag.Title = "Ticket"; 
} 

@Html.DropDownListFor(x => x.CategoryID, 
new SelectList(Model.Category, "Value", "Text")) 

我想要去的地步,我可以参考“TestClient的”,并得到一个下拉列表与

Computers 
Ayyy 
lmao 
+0

做'C#结合下拉谷歌搜索网上xml的file'万吨的例子 – MethodMan 2015-01-26 20:23:51

+0

我已经做了谷歌搜索这事。给出的例子是以对多个列表不适用的方式格式化他们的XML文件。 – 2015-01-26 20:26:11

+2

不应该是'dropDown.Descendants(“猫”)'而不是'dropDown.Descendants(“名称”)'?在“客户”节点下没有称为“名称”的节点,只有名为“猫”的节点。 – juharr 2015-01-26 20:26:12

回答

1

juharr解决了这个问题。

更改:

dropDown.Descendants("name") 

到:

dropDown.Descendants("cat")