How to set DataSource in XML file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a listbox and I want to set the DataSource property to a specific
location of an XML file. Can someone tell me how? I want the DataMember
property to refer to the <ChildTest> section to list all the tests under that
section.

I want to refer either to the <ChildTest> under <TestDefinitions> or the
<ChildTest> under <TestRuns>.


<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CITests>
<TestDefinitions>
<TestGroupName TestName="Application">
<ChildTest>Run Registry Entry</ChildTest>
<ChildTest>Spawn Process</ChildTest>
</TestGroupName>
<TestRuns>
<TestGroupName TestName="Run1.tr">
<ChildTest>Suite1.ts</ChildTest>
<ChildTest>Suite2.ts</ChildTest>
</TestGroupName>
<TestGroupName TestName="Run2.tr">
<ChildTest>Suite2.ts</ChildTest>
</TestGroupName>
</TestRuns>
<TestGroupName TestName="BIOS">
<ChildTest>System Power</ChildTest>
</TestGroupName>
<TestGroupName TestName="Operating System">
<ChildTest>Registry</ChildTest>
</TestGroupName>
<TestGroupName TestName="Storage">
<ChildTest>File Operations</ChildTest>
<ChildTest>File Properties</ChildTest>
</TestGroupName>
<TestGroupName TestName="System">
<ChildTest>Event Logs</ChildTest>
<ChildTest>OCA MRK Files</ChildTest>
<ChildTest>PnP Driver Version Check</ChildTest>
<ChildTest>System Properties Panel</ChildTest>
<ChildTest>WMI</ChildTest>
</TestGroupName>
</TestDefinitions>
</CITests>
 
Hi Steve,

The XmlDocument itself cannot be bound to a ListBox as data source
directly, because it doesn't implement an IList interface. In this case,
you can try to load everything into a DataSet and use the DataSet as
ListBox.DataSource.

Here is an example. It works fine on my machine.

DataSet ds = new DataSet();
ds.ReadXml("..\\..\\XMLFile1.xml");

this.listBox1.DataSource = ds;
this.listBox1.DisplayMember = "ChildTest.ChildTest_Text";

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
This does indeed pull up my list now, but it pulls up all <ChildTests> in the
xml file. How do I filter it to pull up only the section that I desire?
 
Hi Steve,

If this the data in xml needs to be filtered, we cannot put it directly
into DataSet for data binding.

In this case, I suggest you try to use XSLT to transform the xml file,
filter out all the data that is not needed. Or you can load the xml file
into an XmlDocument and remove all the nodes except those <ChildTest>
elements under <TestDefinitions> or the <ChildTest> under <TestRuns>. Then
load the transformed xml into a DataSet and do the data binding.

This seems to be the only way, since we cannot bind to an xml file directly.

Please try this and let me know the results. Thank you!

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Thanks for the response. I read what you stated earlier and it got me to
thinking about serializing Data structures. I created a class that mirrored
the XML data and "deserialized" the XML data into my class. I then used the
class as my DataSource. I was able to move forward quickly after doing that.

Thank you again for triggering some thoughts on this end. :)
 
Nice to know that you have a solution. Glad to help. :-)

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top