Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use repeater with child nodes of XML document

Status
Not open for further replies.

may1hem

Programmer
Jul 28, 2002
262
GB
I'm currently using a repeater to display my business service fees from an XML file. I want to be able to offer 3 pricing options for each service. I want to create child sub-nodes in the XML file, but this doesn't seem to work.

The XML file looks like this now:
-----------------------
<?xml version="1.0" encoding="utf-8" ?>
<services>
<service1>
<id>service1id</id>
<fees>
<payment-option>
<ongoing>
<fee>500</fee>
<period>year</period>
</ongoing>
<extra>
<fee>100</fee>
<period>year</period>
</extra>
</payment-option>
<payment-option>
<ongoing>
<fee>70</fee>
<period>month</period>
</ongoing>
<extra>
<fee>40</fee>
<period>year</period>
</extra>
</payment-option>
<payment-option>
<ongoing>
<fee>1000</fee>
<period>year</period>
</ongoing>
<extra>
<fee>0</fee>
<period>year</period>
</extra>
</payment-option>
</fees>
</service1>
</services>
-----------------------

And the ASP.net file contains the code in the Page_Load event to populate the repeater:
-----------------------
Dim myDataSet As DataSet
Dim strFilePath As String
strFilePath = Server.MapPath("~/services.xml")
'Populate DataSet.
myDataSet = New DataSet()
myDataSet.ReadXml(strFilePath)
Dim xdt2 As DataTable = myDataSet.Tables(0)
myRepeater.DataSource = xdt2
myRepeater.DataBind()
Dim foundRows() As DataRow
-----------------------

And also contains the code to store the XML child node contents in a string:
-----------------------
lsFees1Ongoing = foundRows(0)("services/service1/fees/payment-option/ongoing/fee").ToString
-----------------------

This is where the error occurs:
"Column 'services//service1//fees//payment-option//ongoing//fee' does not belong to table session". I guess this means that I can't refer to the XML child nodes like this. So how can I do this?
 
The .NET parser for XML usually splits data like that into multiple tables. Try checking for multiple tables in the dataset and see if one of them has what you are looking for.
 
the xml data is not tabular. when loading a dataset with xml the data must be tabular.

I would translate the xml data into an object which contains the service id and a collection of payment options.
I would then bind the payment options to the repeater.
here is an example of what the object might look like
Code:
public class Service()
{
   private readonly List<PaymentOption> paymentOptions;

   public Service(int id)
   {
      Id = id;
      paymentOptions = new List<PaymentOption>();
   }

   public int Id {get; private set;}
   public IEnumerable<PaymentOption> PaymentOptions
   {
       get { return paymentOptions; }
   }

   public void AddPaymentOption(Fees fees)
   {
       paymentOptions.Add(new PaymentOption(Fees.OnGoing, Fees.Extra))
   }
}
Fees converts the simple data types from the "on going" and "extra" nodes to OnGoing and Extra objects. Something similar to how Service works. By using private setters and IEnumerable the objects become immutable.

on the xml is converted to a .net object bind the enumeration of Payment Options to the repeater.
Code:
var document = new XmlDocument();
document.LoadXml(xml string);

Service service = MapXmlToService(document);
MyRepeater.DataSource = service.PaymentOptions;
MyRepeater.DataBind();
you provide the code for MapXmlToService(XmlDocument).

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top