2016-05-31 18 views
-2

Ich frage mich, wie diese Linq-Abfrage, die ich in eine Dropdown-Liste in einer C# -Webanwendung geschrieben habe, zu binden. Es ruft eine XML-Datei auf. Ich werde etwas von der XML-Datei posten, aber die 2K-Zeilen wert, also werde ich nur ein Snippet posten.LINQ-Abfrage an Dropdown-Liste binden C#

public void ddlBinding() 
 
     { 
 

 
      // Getting the XML file imported in 
 
      string filePath = @"C:\Projects\MEAS\Central\MillView\PVDA\Main\PVDA\PVDA.Web\MillData.xml"; 
 
      // Set the file path 
 
      XDocument xDoc = XDocument.Load(filePath); 
 

 
      // Setting a variable to dump the data into the dropdownlist 
 
      var dropDownDataList = GetMachineInfo(xDoc, 2); 
 

 

 
     } 
 

 
     // LINQ Queries for binding 
 
     public List<Machine> GetMachineInfo(XDocument xDoc, int machineNumber) 
 
     { 
 
      // Get the elements under a specific machine and create 
 
      // a list of Machine objects 
 
      // Different Linq queries to get the machine data entered 
 
      // This needs to be incorporated for the mill also, this does not get the mill name 
 
      return xDoc.XPathSelectElements("./mmsdata/mill/mach") 
 
           .Where(x => x.Attribute("n").Value == machineNumber.ToString()) 
 
           .Elements() 
 
           .Select(x => new Machine 
 
           { 
 
            sensName = x.Value, 
 
            snsrN = Convert.ToInt32(x.Attribute("n").Value), 
 
            calctype = x.Attribute("calctype").Value 
 
           }).ToList(); 
 
     } 
 

 
     public class Machine 
 
     { 
 
      // Getting all the objects to the machine 
 
      public int snsrN { get; set; } 
 
      public string calctype { get; set; } 
 
      public string sensName { get; set; } 
 

 
     }

<?xml version="1.0" encoding="UTF-8"?> 
 
<mmsdata> 
 
\t <timestamp>Fri Jan 30 08:46:13 EST 2015</timestamp> 
 
\t <mill n="AG" name="Augusta, GA"> 
 
\t \t <ds> 
 
\t \t \t <server>agppra</server> 
 
\t \t \t <server>agpprb</server> 
 
\t \t </ds> 
 
\t \t <mach n="1"> 
 
\t \t \t <srn n="1" calctype="2CV">RL.CLP (1)</srn> 
 
\t \t \t <srn n="3" calctype="2CV">RL.CTWTIR (3)</srn> 
 
\t \t \t <srn n="4" calctype="2CV">RL.CTWTBETA (4)</srn> 
 
\t \t \t <srn n="6" calctype="2CV">RL.DNS (6)</srn> 
 
\t \t \t <srn n="7" calctype="2CV">RL.BSWT (7)</srn> 
 
\t \t \t <srn n="8" calctype="2CV">CA.BSWT (8)</srn> 
 
\t \t \t <srn n="9" calctype="2CV">SP.BSWT (9)</srn> 
 
\t \t \t <srn n="10" calctype="RAW">RL.MST (10)</srn> 
 
\t \t \t <srn n="11" calctype="RAW">CA.MST (11)</srn> 
 
\t \t \t <srn n="12" calctype="RAW">SP.MST (12)</srn> 
 
\t \t \t <srn n="13" calctype="2CV">RL.BNDRWT (13)</srn> 
 
\t \t \t <srn n="14" calctype="2CV">CA.BNDRWT (14)</srn> 
 
\t \t \t <srn n="15" calctype="2CV">SP.BNDRWT (15)</srn> 
 
\t \t \t <srn n="71" calctype="2CV">RL.ICMST (71)</srn> 
 
\t \t \t <srn n="72" calctype="2CV">CA.LATEX (72)</srn> 
 
\t \t \t <srn n="73" calctype="2CV">RL.COLOR (73)</srn> 
 
\t \t \t <srn n="74" calctype="2CV">RL.CLAY (74)</srn> 
 
\t \t \t <srn n="75" calctype="2CV">RL.LATEX (75)</srn> 
 
\t \t \t <srn n="76" calctype="2CV">CA.CLAY (76)</srn> 
 
\t \t \t <srn n="77" calctype="2CV">CA.ICMST (77)</srn> 
 
\t \t \t <srn n="78" calctype="2CV">CA.ICCTWT (78)</srn> 
 
\t \t \t <srn n="91" calctype="2CV">CTRL.HDBOXSP (91)</srn> 
 
\t \t \t <srn n="92" calctype="2CV">CTRL.CTWTSP (92)</srn> 
 
\t \t \t <srn n="93" calctype="2CV">CTRL.HDBOXFB (93)</srn> 
 
\t \t \t <srn n="94" calctype="2CV">CTRL.CTWTFB (94)</srn> 
 
\t \t </mach> 
 
\t \t <mach n="2"> 
 
\t \t \t <srn n="1" calctype="2CV">RL.CLP (1)</srn> 
 
\t \t \t <srn n="4" calctype="2CV">RL.CTWTBETA (4)</srn> 
 
\t \t \t <srn n="7" calctype="2CV">RL.BSWT (7)</srn> 
 
\t \t \t <srn n="8" calctype="2CV">CA.BSWT (8)</srn> 
 
\t \t \t <srn n="9" calctype="2CV">SP.BSWT (9)</srn> 
 
\t \t \t <srn n="10" calctype="RAW">RL.MST (10)</srn> 
 
\t \t \t <srn n="11" calctype="RAW">CA.MST (11)</srn> 
 
\t \t \t <srn n="12" calctype="RAW">SP.MST (12)</srn> 
 
\t \t \t <srn n="13" calctype="2CV">RL.CNDWT (13)</srn> 
 
\t \t \t <srn n="14" calctype="2CV">CA.CNDWT (14)</srn> 
 
\t \t \t <srn n="15" calctype="2CV">SP.CNDWT (15)</srn> 
 
\t \t \t <srn n="81" calctype="RAW">IC.MSTCS (81)</srn> 
 
\t \t \t <srn n="82" calctype="2CV">IC.CTWT1 (82)</srn> 
 
\t \t \t <srn n="84" calctype="RAW">RL.MSTCS (84)</srn> 
 
\t \t \t <srn n="85" calctype="2CV">RL.CTWT2 (85)</srn> 
 
\t \t \t <srn n="86" calctype="2CV">RL.CTWTIR (86)</srn> 
 
\t \t \t <srn n="91" calctype="RAW">CTRL.HDBOXSP (91)</srn> 
 
\t \t \t <srn n="93" calctype="2CV">CTRL.THRMOSP (93)</srn> 
 
\t \t \t <srn n="94" calctype="2CV">CTRL.CTWTSP (94)</srn> 
 
\t \t </mach>

+2

Mögliche Duplikat [Bindungs ​​LINQ-Abfragen und XML Dropdownlists] (http://stackoverflow.com/questions/37484547/binding-linq-queries-and-xml-to-dropdownlists) –

+0

Dies ist dieselbe Frage wie die, die du vor 4 Tagen gestellt hast (http://stackoverflow.com/questions/37484547/binding-linq-queries-and-xml-to-dropdownlists) (und die Frage, die du heute Morgen gestellt hast, dass du ' ve seit gelöscht). Warum macht man das nicht besser? –

Antwort

0

Da Sie bereits eine Sammlung von Elementen, die Sie auf die Bindung an den Dropdown planen, werden Sie müssen einfach nur, um zu bestimmen, welche Eigenschaften sind wird für die und die Value:

verwendet werden
// Bind your Machines to your DropDownList 
MachinesDropDown.DataSource = GetMachineInfo(xDoc, 2); 
// Set your text/value properties 
MachinesDropDown.DataTextField = "snsrN"; 
MachinesDropDown.DataValueField = "orignalPathId"; 
// Actually bind the contents to be reflected in the UI 
MachinesDropDown.DataBind(); 
+0

Dadurch werden nur die Informationen für die hinzugefügt. Ich brauche nicht nur die, die "2" haben. Ich habe mehrere verschiedene, die bis zu 40 reichen. Gibt es eine andere Möglichkeit, diese Zahlen hinzuzufügen, da ich keine Kommas hinzufügen kann, um sie zu trennen? –

0
yourddl.DataSource=dropDownDataList; 
yourddl.DataValueField=<value-property name>; 

yourddl.DataTextField=<text-property name>; 
yourddl.DataBind(); 
Verwandte Themen