0

XElement Doc.

<forms xmlns="">
  <form>
    <id>1361</id>
    <name>TEST3</name>
  </form>
  <form>
    <id>1658</id>
    <name>TEST4</name>
  </form>
  <form>
    <id>1975</id>
    <name>Mac New Patient</name>
  </form>
  <form>
    <id>2209</id>
    <name>Test Atlantic</name>
  </form>
  <form>
    <id>2565</id>
    <name>Rice Creek Test</name>
  </form>
</forms>

Code behind

 XElement xmlForms = data.GetXmlForm();
 var ElementsList = from c in xmlForms.Element("Forms").Descendants("form")
 select new { Name = c.Element("name").Value, ID = c.Element("id").Value };

 cBox_NewPat.DataContext = ElementsList; 
 cBox_NewPat.DisplayMemberPath = "name";
 cBox_NewPat.SelectedValuePath = "id";

I need to bind data(name, id) from XElement to WPF Combobox. For some reason, its not working, not even get the data from XML into the Element List.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jay
  • 15
  • 7
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 02 '13 at 18:44
  • is really as simple as changing .Element("Forms") to .Element("forms")? – Phil Apr 02 '13 at 18:46
  • @Phil, sorry was my mistake, i meant to type "forms", but its not working,, returned null result – Jay Apr 02 '13 at 18:49

2 Answers2

0

The property names are case sensitive.

You need to change

cBox_NewPat.DisplayMemberPath = "name";
cBox_NewPat.SelectedValuePath = "id";

To

cBox_NewPat.DisplayMemberPath = "Name";
cBox_NewPat.SelectedValuePath = "ID";

to match your anonymous type.

Mike Cofoed
  • 151
  • 1
  • 4
  • This is okay, its all in Lowercase.. i got a null exception when i run these codes – Jay Apr 02 '13 at 18:56
  • Sorry, you're right this was the only mistake. got it now Thank You for taking your time to responde – Jay Apr 02 '13 at 20:41
0

It looks like you are missing several things here (aside from the null result - I will get to it below).

  1. you need to set ItemsSource property on the combobox.

    cBox_NewPat.ItemsSource = ElementsList
    
  2. you should use

    cBox_NewPat.DisplayMemberPath = "Name"; 
    

    instead of

    cBox_NewPat.DisplayMemberPath = "name";
    

    since your anonymous type property is called "Name", not "name". The same with SelectedValuePath

  3. please show what you do in GetXmlForm method - this is where something goes wrong. If you do XElement.Parse(xmlString), then it will work if you remove the namespace attribute (xmlns) from the forms element. you will also need to use

    xmlForms.Descendants("form")
    

    instead of

    xmlForms.Element("forms").Descendants("form")
    
anikiforov
  • 495
  • 6
  • 19