472,779 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

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

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>
<filenamefile1.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
Jun 22 '07 #1
4 5584
On Jun 22, 8:36 pm, <kenf...@nospam.nospamwrote:
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>
<filenamefile1.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
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()

Jun 23 '07 #2
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
}
}
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 22, 8:36 pm, <kenf...@nospam.nospamwrote:
>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>
<filenamefile1.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

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()

Jun 23 '07 #3
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

<ke*****@nospam.nospamwrote in message
news:uq**************@TK2MSFTNGP05.phx.gbl...
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
}
}
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
>On Jun 22, 8:36 pm, <kenf...@nospam.nospamwrote:
>>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>
<filenamefile1.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

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()


Jun 23 '07 #4
On Jun 23, 8:02 pm, <kenf...@nospam.nospamwrote:
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

Jun 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mike | last post by:
can somebody please show me some code that will let me populate a checkboxlist from the xmldatasource, and have the text property set to the names in the xml file thanks you can also email me...
0
by: mike | last post by:
<Lookup> <Lookup_Entry> <Ts> <T> <M>context</M> <V>type</V> <Va>Citizen</Va> </T> </Ts> <U>.html</U>
3
by: Brian | last post by:
Using external XML, I'm trying to build a quiz, but I can't seem to specify the DataSource for the RadioButtonList within a Repeater ItemTemplate. I've tried a number of approaches, but I haven't...
1
by: maz01 | last post by:
Hello All, I am developing an application in visual basic 2005 that will run continuously in the background on a users workstation. This application will automatically transfer files from the...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: | last post by:
I am wondering the best and easiest ways to use the VS.NETvisual designer with data controls that will will be loaded programmatically. For example, I have two datalist controls, one of which I'm...
0
by: | last post by:
I'm interested in paging a ASP.NET datalist. The ASP.NET datalist control does not natively support paging. I am binding an XmlDataSource (which is populated by entries in the filesystem) to the...
0
by: =?ISO-8859-1?Q?Arint=E9?= | last post by:
I have an xml that looks like this: <miniblog xmlns:s="miniblog.xsd"> <Blog> <Date>2007-10-10T12:00:00</Date> <Diary>This is a journey into sound 10/10</Diary> </Blog> <Blog>...
0
by: =?ISO-8859-1?Q?Arint=E9?= | last post by:
I have an xml that looks like this: <miniblog xmlns:s="miniblog.xsd"> <Blog> <Date>2007-10-10T12:00:00</Date> <Diary>This is a journey into sound 10/10</Diary> </Blog> <Blog>...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.