Custom Dataview from XML Dataset

I have a dataset the I get back from an FORM POST...it's in XML format. (Attached code)
I need to build a custoim tableview so that I can output into a gridview

here's the (columns) layout I need from the XML below (it will be multiple rows of course...the data below will return 2 rows.)

resortID  resortName   nbrOfBedrooms   checkInDate   checkoutDate   value
<accommodationItem>
            <resortId>0450</resortId>
            <resortName>Sheraton Vistana Resort</resortName>
            <nbrOfBedrooms>2</nbrOfBedrooms>
            <nbrOfBathrooms>0.0</nbrOfBathrooms>
            <kitchenType>Full Kitchen</kitchenType>
            <maxOccupancy>8</maxOccupancy>
            <privacyOccupancy>6</privacyOccupancy>
            <availabilityInfo>
                <systemId>
                    <inventoryType>1</inventoryType>
                    <inventoryId>rNnn6T4Gwjd6r9sj4w+3CXgtWrNbxbMWJzoakGRFDrTEhWAqo2rI4ALOtq+vOkUx</inventoryId>
                </systemId>
                <checkInDate>05/30/2009</checkInDate>
                <checkOutDate>06/06/2009</checkOutDate>
                <unitCost>
                    <value>325.00</value>
                    <currency>USD</currency>
                </unitCost>
                <unitCount>1</unitCount>
            </availabilityInfo>
            <availabilityInfo>
                <systemId>
                    <inventoryType>1</inventoryType>
                    <inventoryId>rNnn6T4Gwje0Lt74wo40nXgtWrNbxbMWJzoakGRFDrTEhWAqo2rI4ALOtq+vOkUx</inventoryId>
                </systemId>
                <checkInDate>05/31/2009</checkInDate>
                <checkOutDate>06/07/2009</checkOutDate>
                <unitCost>
                    <value>325.00</value>
                    <currency>USD</currency>
                </unitCost>
                <unitCount>2</unitCount>
            </availabilityInfo>
        </accommodationItem>

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wht1986Commented:
Simple looping through example bound to gridview
public class Test
{
    public string resortId { get; set; }
    public string resortName { get; set; }
    public string nbrOfBedrooms { get; set; }
    public string checkInDate { get; set; }
    public string checkoutDate { get; set; }
    public string value { get; set; }
}
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String xml = @"Your XML String Here";
 
        // load xml string into dataset
        StringReader sr = new StringReader(xml);
        DataSet ds = new DataSet();
        ds.ReadXml(sr);
 
        List<Test> list = new List<Test>();
 
        foreach (DataRow accomRow in ds.Tables["accommodationItem"].Rows)
        {
            foreach (DataRow availRow in accomRow.GetChildRows("accommodationItem_availabilityInfo"))
            {
                foreach (DataRow unitRow in availRow.GetChildRows("availabilityInfo_unitCost"))
                {
                    Test t = new Test();
                    t.resortId = accomRow["resortId"].ToString();
                    t.resortName = accomRow["resortName"].ToString();
                    t.nbrOfBedrooms = accomRow["nbrOfBedrooms"].ToString();
                    t.checkInDate = availRow["checkInDate"].ToString();
                    t.checkoutDate = availRow["checkoutDate"].ToString();
                    t.value = unitRow["value"].ToString();
                    list.Add(t);
                }
            }
        }
 
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
}

Open in new window

0
wht1986Commented:
I know we spoke before, if you are using .net 3.5 you can use LINQ to XML like below
XDocument doc = XDocument.Parse(xml);
 
        var list = from cost in doc.Elements("accommodationItem").Elements("availabilityInfo").Elements("unitCost")
                   select new
                   {
                       ResortID = cost.Parent.Parent.Element("resortId").Value,
                       ResortName = cost.Parent.Parent.Element("resortName").Value,
                       NumberOfBedrooms = cost.Parent.Parent.Element("nbrOfBedrooms").Value,
                       CheckInDate = cost.Parent.Element("checkInDate").Value,
                       CheckOutDate = cost.Parent.Element("checkOutDate").Value,
                       Value = cost.Element("value").Value
                   };
 
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();

Open in new window

0
Larry Bristersr. DeveloperAuthor Commented:
wht1986:
That definately looks right...
But when I convert to VB I'm a little clueless as to what goes where.

The public class test had some "blue underlines" on conversion.

And the grid built with column headers...but no data.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

wht1986Commented:
VB ick (just kidding)  Which version do you want, the looping or LINQ to XML?
0
wht1986Commented:
Here's the VB version of the looping, using a temp datatable instead of the class
Imports System.Data
Imports System.IO
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim xml As String = "<accommodationItem><resortId>0450</resortId><resortName>Sheraton Vistana Resort</resortName>" + _
            "<nbrOfBedrooms>2</nbrOfBedrooms><nbrOfBathrooms>0.0</nbrOfBathrooms><kitchenType>Full Kitchen</kitchenType>" + _
            "<maxOccupancy>8</maxOccupancy><privacyOccupancy>6</privacyOccupancy><availabilityInfo><systemId>" + _
            "<inventoryType>1</inventoryType><inventoryId>rNnn6T4Gwjd6r9sj4w+3CXgtWrNbxbMWJzoakGRFDrTEhWAqo2rI4ALOtq+vOkUx</inventoryId>" + _
            "</systemId><checkInDate>05/30/2009</checkInDate><checkOutDate>06/06/2009</checkOutDate><unitCost><value>325.00</value>" + _
            "<currency>USD</currency></unitCost><unitCount>1</unitCount></availabilityInfo><availabilityInfo><systemId>" + _
            "<inventoryType>1</inventoryType><inventoryId>rNnn6T4Gwje0Lt74wo40nXgtWrNbxbMWJzoakGRFDrTEhWAqo2rI4ALOtq+vOkUx</inventoryId>" + _
            "</systemId><checkInDate>05/31/2009</checkInDate><checkOutDate>06/07/2009</checkOutDate><unitCost><value>325.00</value>" + _
            "<currency>USD</currency></unitCost><unitCount>2</unitCount></availabilityInfo></accommodationItem>"
 
        Dim sr As StringReader = New StringReader(xml)
        Dim ds As DataSet = New DataSet()
        ds.ReadXml(sr)
 
        'create a temp datatable
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("ResortId")
        dt.Columns.Add("ResortName")
        dt.Columns.Add("Beds")
        dt.Columns.Add("CheckIn")
        dt.Columns.Add("CheckOut")
        dt.Columns.Add("Value")
 
        For Each accomRow As DataRow In ds.Tables("accommodationItem").Rows
            For Each availRow As DataRow In accomRow.GetChildRows("accommodationItem_availabilityInfo")
                For Each unitRow As DataRow In availRow.GetChildRows("availabilityInfo_unitCost")
                    dt.Rows.Add(New Object() {accomRow("resortId"), accomRow("resortName"), accomRow("nbrOfBedrooms"), availRow("checkInDate"), availRow("checkOutDate"), unitRow("value")})
                Next
            Next
        Next
 
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
Perfect...thanks for sticking with me
  I'll be posting a follow-up question
0
Larry Bristersr. DeveloperAuthor Commented:
wht1986:

You really came through for me...greatly appreciated.
0
Larry Bristersr. DeveloperAuthor Commented:
wht1986
Can you look at http://www.experts-exchange.com/Microsoft/Development/Q_24452209.html for me?

I can't pull in the inventoryID from the systemID table

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.