C# and XML

Hello,

I am new to C# and new to this board... I am trying to write a little tiny ASP page (C# code behind) that would take me about 15 minutes in ColdFusion. That said, I am not using CF, but C# is kicking my butt. Here's what I am trying to do:

I have an XML file (3 of the 18 lines are below)

<?xml version="1.0" encoding="UTF-8"?>










I need to read from this file and output the results in a table on an ASP page (C# behind). I will not be writing back to the file, reading only. The 'dog pic' field is the image name without the .jpg at the end, I need the picture in the first column, the breed in the second and the info in the third, and for the life of me I can not get it to work. I tried an array and a databinding and about 5 other things and now I don't get any errors, but I don't get output either.

Any help would be appreciated.

Thanks very much,

Michelle

Comments

  • It's quite easy once you get the hang of it

    Here are the steps:

    1. New ASP.NET application.
    2. In your form, add a data grid, say DataGrid1.
    3. Go to the code behind page, in your Page_Load event, enter this code:
    [code]
    if(!IsPostBack)
    {
    DataSet ds = new DataSet("Dogs");
    // make sure to fix this to the real path to dogs.xml
    ds.ReadXml(@x:full pathdogs.xml);
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
    }
    [/code]
    Run, see that it works. And that's all the code you need.

    Although the default data binding works fine so far, the data grid aint pretty, lets fix that:

    1. Goto the properties of the data grid, change "AutoGenerateColumns" to false.
    2. Now we're gonna generate those columns manually, right click the data grid and select "Property Builder"
    3. Select "Columns" from the left side, you want the first column to be the picture, so select "Bound Column" as its type, set the header text to "Image" and data field to "dogPic".
    4. The next column is the breed, so add a "Bound Column", with Header text to "Breed" and data field to "dogBreed".
    5. Next is info, so repeat with "Info" and "dogInfo".

    Run, see that it works again, time to make the image field:

    1. Right click the data grid again, right click the data grid, Property Builder, select the Image column, click the "Convert this column into a template column". and hit OK. Now, you have a real HTML table for each row in this column that you can add whatever controls you want to it.
    2. right click the data grid again, select edit template, template 0 (image). VS.NET should show you the template of the column, you can now drag drop controls over to the various column items.
    3. First thing, select the label from the ItemTemplate, go to properties, click the ... next to DataBinding and copy the text from the custom binding expression of its Text property. This is how you get the text from the XML file. So save it.
    4. Delete the textbox and the label from the template, you don't need them, drag an Image control from the Web Forms toolbox into the ItemTemplate part
    5. go to the properties of the image, data binding, click the ..., select ImageUrl and enter the text you copied from the label data binding, should be something like this:
    DataBinder.Eval(Container, "DataItem.dogPic")
    This is the info from your XML file, so change it to the actual location on the images on the server, something like this:
    "http://www.dogs.com/images/"+DataBinder.Eval(Container, "DataItem.dogPic")+".jpg"
    Makes sense huh?
    6. Right click the template and "end editing template.

    Run, it should work

    : Hello,
    :
    : I am new to C# and new to this board... I am trying to write a little tiny ASP page (C# code behind) that would take me about 15 minutes in ColdFusion. That said, I am not using CF, but C# is kicking my butt. Here's what I am trying to do:
    :
    : I have an XML file (3 of the 18 lines are below)
    :
    : <?xml version="1.0" encoding="UTF-8"?>
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    : I need to read from this file and output the results in a table on an ASP page (C# behind). I will not be writing back to the file, reading only. The 'dog pic' field is the image name without the .jpg at the end, I need the picture in the first column, the breed in the second and the info in the third, and for the life of me I can not get it to work. I tried an array and a databinding and about 5 other things and now I don't get any errors, but I don't get output either.
    :
    : Any help would be appreciated.
    :
    : Thanks very much,
    :
    : Michelle
    :
    :

  • Thank you SOOO much! It works perfictly... and the code looks much nicer than the array and DataTable I was 'trying' to build!!

    Michelle
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion