By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

will this code work????

P: n/a
public string GetFile(string objXml)
{
XmlDocument xmlINPUT = new XmlDocument();
xmlINPUT.LoadXml(objXml);
string directoryName =
xmlINPUT.GetElementsByTagName("DIRECTORY")[0].Value.ToString();
string fileName =
xmlINPUT.GetElementsByTagName("FILE")[0].Value.ToString();

XmlDocument xmlOUTPUT = new XmlDocument();
XmlElement xmlROOT = xmlOUTPUT.CreateElement("ROOT");
xmlOUTPUT.AppendChild(xmlROOT);
XmlElement xmlFILE = xmlOUTPUT.CreateElement("FILE");
xmlFILE.SetAttribute("NAME",fileName);

/* Conversion of file into byte stream STARTS */
Byte[] byteFILE;
FileStream fs = new
FileStream("\\"+directoryName+"\\"+fileName,System .IO.FileMode.Open,System.IO.FileAccess.Read);
byteFILE = new byte[fs.Length];
BufferedStream bs = new
BufferedStream(fs,Convert.ToInt32(fs.Length));
char[] charFILE = new char[byteFILE.Length];
for(int i = 0; i < fs.Length; i++)
{
byteFILE[i] = (byte)bs.ReadByte();
charFILE[i] = Convert.ToChar(byteFILE[i]);
}
string stringFILE = charFILE.ToString();
/* Conversion of file into byte stream ENDS */
xmlFILE.AppendChild(xmlOUTPUT.CreateCDataSection(s tringFILE));
xmlROOT.AppendChild(xmlFILE);
return xmlOUTPUT.InnerXml.ToString();
}

Sep 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

The function accepts xml string which is:
<ROOT>
<DIRECTORY NAME="DOCUMENTS">
<FILE>file1.txt</FILE>
</DIRECTORY>
</ROOT>

and takes this file from drive and converts it into a character array
to be fed into CDATA of xml.
i want to know if it's written correctly and whether it will work.

Sep 27 '06 #2

P: n/a
The questions are just begging to be asked:

Have you tried it?

If not, why not?

If so, does it?

"Saurabh" <on***********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
The function accepts xml string which is:
<ROOT>
<DIRECTORY NAME="DOCUMENTS">
<FILE>file1.txt</FILE>
</DIRECTORY>
</ROOT>

and takes this file from drive and converts it into a character array
to be fed into CDATA of xml.
i want to know if it's written correctly and whether it will work.

Sep 27 '06 #3

P: n/a

Saurabh wrote:
The function accepts xml string which is:
<ROOT>
<DIRECTORY NAME="DOCUMENTS">
<FILE>file1.txt</FILE>
</DIRECTORY>
</ROOT>

and takes this file from drive and converts it into a character array
to be fed into CDATA of xml.
i want to know if it's written correctly and whether it will work.
At least four problems with your code:

1) You're assuming an ISO-8859-1 encoding given the way you're reading
the data
2) You're making a false assumption about what char[].ToString does.
3) If the file length changes, you carry on reading
4) You're never closing the streams, which you should be doing
automatically with a "using" statement

Why not use a StreamReader to read the data from the file? It looks
like all you're trying to do in that section is read the text from a
file, which can be done with StreamReader.ReadToEnd much more simply.

Jon

Sep 27 '06 #4

P: n/a

Thanks Jon,i'll try the StremReader.

Sep 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.