Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

How to programmatically create and populate an XmlDataSource with info from filesystem?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kenfine@nospam.nospam - 22 Jun 2007 19:36 GMT
Hi all,

I want to create a method that does the following:
1) Programmatically instantiate a new XmlDataSource control
2) For each file in a named directory, make a "FileSystemItem" element
3) On each FileSystemItem Element, make two child nodes, one with the file
name, one with the file size. ie.

<filesystemitems>
       <filesystemitem>
               <filename> file1.jpg</filename>
               <filesize>48935835</filesize>
        </filesystemitem>
       <filesystemitem>

I'll bind the resultant XmlDataSource to various controls.

Can someone suggest generally the syntax I want? I know how to loop over
directory items via foreach, but a quick look over MSDN did not suggest how
to instantiate the XmlDataSource control or any kind of .Add method I could
use to populate it.

Thanks!
-KF
Alexey Smirnov - 23 Jun 2007 15:25 GMT
On Jun 22, 8:36 pm, <kenf...@nospam.nospam> wrote:
> Hi all,
>
[quoted text clipped - 20 lines]
> Thanks!
> -KF

something like this

Dim dir As New IO.DirectoryInfo(Server.MapPath("/"))
Dim fis As IO.FileInfo() = dir.GetFiles()
Dim fi As IO.FileInfo

Dim s As New IO.MemoryStream()

Dim xw As New System.Xml.XmlTextWriter(s, Encoding.UTF8)
With xw
.WriteStartDocument()
.WriteStartElement("filesystemitems")

For Each fi In fis
.WriteStartElement("filesystemitem", Nothing)
.WriteStartElement("filename", Nothing)
.WriteString(fi.Name)
.WriteEndElement()
.WriteStartElement("filesize", Nothing)
.WriteString(fi.Length)
.WriteEndElement()
.WriteEndElement()
Next fi

.WriteEndElement()
End With
xw.Flush()
kenfine@nospam.nospam - 23 Jun 2007 18:45 GMT
Thank you, Alexey. Your code taught me a lot. For anyone who's interested, I
converted this to C#. See below

However, I am new to the MemoryStream, and am having trouble converting my
MemoryStream object to a byte array and then to a string, which I want to
response.write to the screen. Could someone please look at my code below and
fill in what needs to happen in my commented area?

Thanks again,
-KF

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;

public partial class Photo_FilesystemToXmlDataSource : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {

       DirectoryInfo dir = new DirectoryInfo(@"c:\MyPhysicalPath");
       FileInfo[] fis = dir.GetFiles();
       //FileInfo fi = new FileInfo();

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

       foreach (FileInfo fi in fis)
       {
           xw.WriteStartElement("filesystemitem");
           xw.WriteStartElement("filename");
           xw.WriteString(fi.Name);
           xw.WriteEndElement();
           xw.WriteStartElement("filesizeA");
           xw.WriteString(Convert.ToString(fi.Length));
           xw.WriteEndElement();
           xw.WriteEndElement();
       }
       xw.WriteEndElement();
       xw.Flush();

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

> On Jun 22, 8:36 pm, <kenf...@nospam.nospam> wrote:
>> Hi all,
[quoted text clipped - 52 lines]
> End With
> xw.Flush()
kenfine@nospam.nospam - 23 Jun 2007 19:02 GMT
I figured out how to write the file to the filesystem by first casting it to
a byte array and saving it using a FileStream object. I then  convert my
byte array to a string using System.Text.Encoding.UTF8 and response.write
it. I read that casting to a byte array is not the most efficient way to do
this. How could I improve this code to be more performant? :

       // Write memorystream to a file. This is not the most efficient way
to do this. What's better?
       FileStream fs = File.OpenWrite("c:\\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);

Thanks -
-KF

> Thank you, Alexey. Your code taught me a lot. For anyone who's interested,
> I converted this to C#. See below
[quoted text clipped - 112 lines]
>> End With
>> xw.Flush()
Alexey Smirnov - 23 Jun 2007 22:29 GMT
On Jun 23, 8:02 pm, <kenf...@nospam.nospam> wrote:
> I figured out how to write the file to the filesystem by first casting it to
> a byte array and saving it using a FileStream object. I then  convert my
> byte array to a string using System.Text.Encoding.UTF8 and response.write
> it. I read that casting to a byte array is not the most efficient way to do
> this. How could I improve this code to be more performant? :

I think you can use a IO.StreamReader()

xw.Flush();
//???? How do I convert my MemoryStream object to a string and
response.write it here?

s.Position = 0;
StreamReader sr = new StreamReader(s);
Response.Write(sr.ReadToEnd());

Hope it helps

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.