I have the following XML,
<?xml version="1.0" encoding="utf-8"?>
<TEST>
<Name>TESTRUN</Name>
<SyncByte>ff</SyncByte>
<SOM>53</SOM>
<PDADD>7e</PDADD>
<LENLSB>08</LENLSB>
</TEST>
I have defined a listbox in WPF and have simply added a single checkbox inside it hoping that it can manage the checkboxes dynamically. The requirement is that, I can have multiple XMLs like the one above but with different tag in them. I need to load the ListBox with the value of the tag from each XML, as a checkbox.
Design:
As first you need to create a TEST class
public class TEST
{
public string Name { get; set; }
public string PDADD { get; set; }
}
You can load the XML file with the XML serializer:
TEST obj;
XmlSerializer xs = new XmlSerializer(typeof(TEST));
using (XmlReader xr = XmlReader.Create("yourxmlfile.xml"))
{
obj = (TEST)xs.Deserialize(xr);
}
After that, attach obj
to the DataSource
of your Checkbox-List and use DataBind()
of it finally. The markup of the Checkbox-List should set the desired DataValueField
and DataTextField
to "Name" (property Name
of TEST). Can your XML contain duplicated item names, you have to provide any ID in your XML, too. In that case, set DataValueField
to "ID".
Also I suggest you to follow the standards for upper and lowercase in your names. So change "TEST" to "Test" and "PDADD" to "PDAdd".