创建DropdownList

时间:2014-12-25 00:04:37

标签: asp.net xml linq html.dropdownlistfor

考虑以下XML文件:

   <?xml version="1.0" encoding="utf-8"?>
            <warehouse>
              <cat id="computer">
                <item>
                  <SN>1</SN>
                  <name>Toshiba</name>
                  <quantity>12</quantity>
                  <description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
                  <price>400 USD</price>
                </item>
                <item>
                  <SN>2</SN>
                  <name>Dell</name>
                  <quantity>14</quantity>
                  <description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
                  <price>300 USD</price>
                </item>
                <item>
                  <SN>3</SN>
                  <name>Dell</name>
                  <quantity>14</quantity>
                  <description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
                  <price>300 USD</price>
                </item>
                <item>
                  <SN>5</SN>
                  <name>Dell</name>
                  <quantity>14</quantity>
                  <description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
                  <price>300 USD</price>
                </item>
                <item>
                  <SN>6</SN>
                  <name>Dell</name>
                  <quantity>14</quantity>
                  <description>CPU: CORE I5 RAM: 3 GB HD: 512 GB</description>
                  <price>300 USD</price>
                </item>
                <item>
                  <SN>8</SN>
                  <name>Toshiba</name>
                  <quantity>13</quantity>
                  <description>CPU: CORE I5 RAM: 5 GB HD: 512 GB3</description>
                  <price>400 USD</price>
                </item>
                <item>
                  <SN>9</SN>
                  <name>Toshiba</name>
                  <quantity>13</quantity>
                  <description>CPU: CORE I5 RAM: 5 GB HD: 512 GB3</description>
                  <price>400 USD</price>
                </item>
                <item>
                  <SN>dsfdSF</SN>
                  <name>fsdfsD</name>
                  <quantity>dsfdSFS</quantity>
                  <description>FSDFS</description>
                  <price>FSDFSD</price>
                </item>
                <item>
                  <SN>dsfdSF</SN>
                  <name>fsdfsD</name>
                  <quantity>dsfdSFS</quantity>
                  <description>FSDFS</description>
                  <price>FSDFSD</price>
                </item>
                <item>
                  <SN>alia</SN>
                  <name>alia</name>
                  <quantity>alia</quantity>
                  <description>alia</description>
                  <price>alia</price>
                </item>
              </cat>
              <cat id="Stationery">
                <item>
                  <SN> 1 </SN>
                  <name>note books</name>
                  <quantity>250</quantity>
                  <description>Caterpiller</description>
                  <price>5 USD</price>
                </item>
                <item>
                  <SN> 2 </SN>
                  <name> pencils </name>
                  <quantity> 300 </quantity>
                  <description> Caterpiller </description>
                  <price> 2 USD </price>
                </item>
                <item>
                  <SN> 3 </SN>
                  <name> note books </name>
                  <quantity> 250 </quantity>
                  <description> Caterpiller  </description>
                  <price> 5 USD </price>
                </item>
                <item>
                  <SN>4</SN>
                  <name>pencils</name>
                  <quantity>45</quantity>
                  <description>Pilot</description>
                  <price>4 USD</price>
                </item>
                <item>
                  <SN>5</SN>
                  <name>pencils</name>
                  <quantity>45</quantity>
                  <description>Pilot</description>
                  <price>4 USD</price>
                </item>
                <item>
                  <SN>6</SN>
                  <name>pencils</name>
                  <quantity>45</quantity>
                  <description>Pilot</description>
                  <price>4 USD</price>
                </item>
                <item>
                  <SN>7</SN>
                  <name>sdfsdfsdfsdfsd</name>
                  <quantity>sdfsdf</quantity>
                  <description>dsfsdfs</description>
                  <price>sdfsdf</price>
                </item>
                <item>
                  <SN>8</SN>
                  <name>pencils</name>
                  <quantity>45</quantity>
                  <description>Pilot</description>
                  <price>4 USD</price>
                </item>
                <item>
                  <SN>9</SN>
                  <name>books</name>
                  <quantity>250</quantity>
                  <description>chinses</description>
                  <price>3 USD</price>
                </item>
                <item>
                  <SN>alia</SN>
                  <name>alia</name>
                  <quantity>alia</quantity>
                  <description>alia</description>
                  <price>alia</price>
                </item>
              </cat>
              <cat id="Furniture">
                <item>
                  <SN> 1 </SN>
                  <name>dasd</name>
                  <quantity>asdasd</quantity>
                  <description>das</description>
                  <price>dasd</price>
                </item>
                <item>
                  <SN> 2 </SN>
                  <name> chairs </name>
                  <quantity> 18 </quantity>
                  <description> European Type</description>
                  <price> 150 USD </price>
                </item>
                <item>
                  <SN>3</SN>
                  <name>Tabels</name>
                  <quantity>12</quantity>
                  <description>European Type</description>
                  <price>50 USD</price>
                </item>
                <item>
                  <SN>4</SN>
                  <name>Tabels</name>
                  <quantity>12</quantity>
                  <description>European Type</description>
                  <price>50 USD</price>
                </item>
                <item>
                  <SN>5</SN>
                  <name>Tabels</name>
                  <quantity>12</quantity>
                  <description>European Type</description>
                  <price>50 USD</price>
                </item>
                <item>
                  <SN>6</SN>
                  <name>Tabels</name>
                  <quantity>12</quantity>
                  <description>European Type</description>
                  <price>50 USD</price>
                </item>
                <item>
                  <SN>7</SN>
                  <name>Tabels</name>
                  <quantity>12</quantity>
                  <description>European Type</description>
                  <price>50 USD</price>
                </item>
                <item>
                  <SN>8</SN>
                  <name>Tabels</name>
                  <quantity>12</quantity>
                  <description>European Type</description>
                  <price>50 USD</price>
                </item>
                <item>
                  <SN>alia</SN>
                  <name>alia</name>
                  <quantity>alia</quantity>
                  <description>alia</description>
                  <price>alia</price>
                </item>
              </cat>
            </warehouse>

我需要创建一个下拉列表。下拉列表必须仅显示<SN> <cat id="computer">的所有值。 如何使用LINQ或DataView或Xpath完成此操作?

2 个答案:

答案 0 :(得分:0)

将xml加载到XElement中:

        var xml = XElement.Load("test.xml");

执行XPath以选择具有id计算机的猫中的SN元素:( +将它们放入列表中)

        var snValues = xml.XPathSelectElements("//cat[@id='computer']/item/SN")
                        .Select(x => x.Value).ToList();

必需的用法:

        using System.Linq;
        using System.Xml.Linq;
        using System.Xml.XPath;

答案 1 :(得分:0)

这个怎么样?

        XmlDocument doc = new XmlDocument();
        doc.Load(@"~/XML/XML.xml");
        var nodes = doc.SelectNodes("warehouse/cat[@id='computer']/item/SN");

        myDropDown.DataTextField = "InnerText";
        myDropDown.DataValueField = "InnerText";


        //now bind the dropdownlist to the dataview
        myDropDown.DataSource = nodes;
        myDropDown.DataBind();