will this code work????

S

Saurabh

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 = (byte)bs.ReadByte();
charFILE = Convert.ToChar(byteFILE);
}
string stringFILE = charFILE.ToString();
/* Conversion of file into byte stream ENDS */
xmlFILE.AppendChild(xmlOUTPUT.CreateCDataSection(stringFILE));
xmlROOT.AppendChild(xmlFILE);
return xmlOUTPUT.InnerXml.ToString();
}
 
S

Saurabh

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.
 
S

Stephany Young

The questions are just begging to be asked:

Have you tried it?

If not, why not?

If so, does it?
 
J

Jon Skeet [C# MVP]

Saurabh said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top