Saturday, July 21, 2007

example | C# .NET ASP.NET | Writing a directory's contents as XML, and binding that XML to an ASP.NET databound control

Intent: Write an XML file that describes each file in a directory, and then use that XML file as a datasource for an ASP.NET databound control.

Discussion: .NET has high-level abstractions for creating XML in the form of the XmlTextWriter object. It is fairly easy to use.

Overview: Instantiate a DirectoryInfo Object. Instantiate a FileInfo array by calling the .GetFiles on your instantiated DirectoryInfo object. Instantiate a MemoryStream object. Instantiate an XmlTextWriter object and set its character encoding to UTF-8. Initate a foreach loop over the FileInfo[] array, instatiating a fileInfo object on each iteration of the loop. To open a new element and populate it, call XmlTextWriterObject.WriteStartElement, then call XmlTextWriterObject.WriteString  to assign the content of the element, and then call XmlTextWriterObject.WriteEndElement() to close it. Note that elements can be nested; be sure to call the .EndElement() method at the appropriate place for your nest. Write the finished Xml to a file by instantiating a FileWriter object, calling OpenWrite(Filename), instantiating a byte[] array and calling the MemoryStream.ToArray() method, and finally calling the FileStream.Write() using the byte array as a parameter of the method. Once the XML is written, specify it as an XmlDataSource by assigning the byte array to the XmlDataSource.Data property and calling the XmlDataSource.DataBind() method. (whew!)

Weaknesses: I'd like to know how to avoid writing the Xml to a temporary file, as shown below. How can this all happen in memory?

 

   protected void ShowPhotoGrid( string jobID)
{

DirectoryInfo dir = new DirectoryInfo(@"R:\whatever\"+jobID);
FileInfo[] fis = dir.GetFiles("*ld200.jpg");
//FileInfo fi = new FileInfo();

MemoryStream s = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(s,Encoding.UTF8);
xw.WriteStartDocument();
xw.WriteStartElement("filesystemitems");
string largefilename = "";

foreach (FileInfo fi in fis)
{
xw.WriteStartElement("filesystemitem");
xw.WriteStartElement("filename");
xw.WriteString(fi.Name);
xw.WriteEndElement();
xw.WriteStartElement("filenameandpath");
xw.WriteString("http://whatever.org/content/whatever/" + jobID +"/" + fi.Name);
xw.WriteEndElement();
largefilename = StringManipulation.LeftOfRightmostOf(fi.Name, '_');
largefilename = (largefilename + "_ld600_watermark.jpg");
xw.WriteStartElement("filenameandpathlarge");
xw.WriteString("http://whatever.org/content/whatever/" + jobID + "/" + largefilename);
xw.WriteEndElement();
xw.WriteStartElement("filesize");
xw.WriteString(Convert.ToString(fi.Length));
xw.WriteEndElement();
xw.WriteEndElement();
}
xw.WriteEndElement();
xw.Flush();

// Write memorystream to a file. This is not the most efficient way to do this. What's better?
FileStream fs = File.OpenWrite(@"R:\whatever\blahblah.txt");
byte[] data = s.ToArray();
fs.Write(data, 0, data.Length);
fs.Close();

// Convert the data array to a string and response.write it
string myString = System.Text.Encoding.UTF8.GetString(data);
// Response.Write(myString);


XmlDataSource1.Data = myString;
XmlDataSource1.DataBind();
DataList1.DataBind();

}
//???? How do I convert my MemoryStream object to a string
        // It involves Writing the MemoryStream to a byte array, and converting it to a string, but I can't get things working quite right
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home