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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a label from XML 1

Status
Not open for further replies.

barrylowe

Programmer
Nov 6, 2001
188
0
0
GB
I have a web page which uses a GridView to display the contents of an XML file.

The grid is bound to the XML file using an XMLDataSource control and that all works fine.

The XML fle looks something like this:

<FOI_Enquiries CreatedOn = "12-Aug-08">
<Enquiry EnquiryID="89"
DateLogged="07-Nov-2006"
DateLoggedX="20061107"
Subject="Barrys test"
Question="FOI Requests"
Response="second Final Response test">
<AttachedDocuments>
<Document DocumentName="RefreshTest.pdf"
DisplayName="My test pdf document" />
<Document DocumentName="Test1.doc"
DisplayName="" />
<Document DocumentName="Test2.doc"
DisplayName="My second test Word Document" />
<Document DocumentName="Test3.xls"
DisplayName="My first Excel test" />
<Document DocumentName="ClosedTest1.doc"
DisplayName="Closed Tester" />
</AttachedDocuments>
</Enquiry>
<Enquiry EnquiryID="91"
DateLogged="09-Jan-2007"
DateLoggedX="20070109"
Subject="Health Promotion"
Question="not exclusion test"
Response="dsfsfsdf">
<AttachedDocuments>
<Document DocumentName="Test3.xls"
DisplayName="" />
</AttachedDocuments>
</Enquiry>
</FOI_Enquiries>

The gridview displays each of the Enquiry elements. Where I am having the problme is that I want a lable on my page to display the value of the CreatedOn attribute of the root element.

What is the easiest way to do this?
 
load the xml into an xml document. pass the xmldocument's text to the xmldatasource control's text property (instead of setting the file location directly in the datasource control).
use an xpath query to pull the created on value. place this text in the label.

something like this
Code:
XmlDocument xml = new XmlDocument();
xml.Load("path to file");
MyXmlDatasource.Text = xml.OutterXml;
MyGrid.DataBind(); //not sure if you need this
MyLabel.Text = xml.GetValue("/[@CreatedOn]");

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks very much for that, it set me on the correct path for my solution.

My final code was:

Dim XMLdoc As XmlDocument = New XmlDocument() XMLdoc.Load(Server.MapPath("XMLfiles/FOIrequests.xml"))
Me.lblDateUpdated.Text = XMLdoc.SelectSingleNode("/FOI_Enquiries/@CreatedOn").Value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top