0

Is there anyway i can call orderedFiles in my asp xml tags so that it displays the file associated with orderedFiles?

EDIT

Is it possible to replace the current filepath with a value in an xml document?

1 Answer 1

1

You can use databinding to set the value of the DocumentSource property at databind-time. First, in your code-behind, create a public or protected method that returns a string that contains the physical path to the file you want to display. In your case, this method would have the three lines of code you listed, and would return orderedFiles.FullName, which is the physical path to the newest file. Let's say this method is called "GetFile()", and it should look something like this:

Public Function GetFile() As String
    Dim di As New DirectoryInfo("C:\Users\Simon\Desktop\XML Logs\")
    Dim files As FileSystemInfo() = di.GetFileSystemInfos()
    Dim newestFile = files.OrderByDesc(Function(f) f.CreationTime).First
    Return newestFile.FullName
End Sub

Then In your markup, use databinding syntax to assign the DocumentSource property to the output of this method call:

<asp:Xml ID="xmlControl" runat="server" DocumentSource='<%# GetFile() %>' />

Finally, you will need to make sure you kick off databinding on the page by calling Page.DataBind() at some point in your page load method.

If you want a good introduction to databinding, you can see this article by Dino Esposito

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help, i seem to be having a bit of trouble, i have updated what i have done so far, any pointers would be much appreciated!
I think in VB "Public Sub" is the equivalent of a void C# method. You want the method to return a string values, so you should use "Public Function" and then the Return keyword. I updated by answer to show what the method should look like.
Seems that changing it to a public function made a heck of a difference! Thank you so much for you help!
Is it possible to use the same concept but to set the filepath to a value of an xml node as opposed to a pre-defined value?
I think the DocumentSource attribute of the asp:XML server control must be set to an entire XML file. If you just want the control to be bound to a fragment of an XML document you might want to use the DocumentContent property, which can be bound to an XML string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.