Introduction
There was a need to have a web service send a file. The documented way to do this is through WSE (Web Service Extension), which is an add-on to the .NET Framework to do things with web services like attachments. Using two CodeProject articles, I added zipping the file and encoding the file using Base64. Then I placed the encoded file into a CDATA node in an XML document.
Background
As I looked into sending a file via a web service, I was not pleased with the amount of work that had to be done to attach a file using WSE. So I started to look for other options. One of my co-workers suggested using a CDATA node in an XML document. As I looked into the CDATA node, it seemed like it might be a good fit. The CDATA node is for storing blob data in an XML file. It is marked so that it ignores what is between the CDATA nodes. I decided to zip the file to reduce the size of the XML file I would be sending. The problem is that zipping a file produces some characters that cause problems with the XML file. So, that leads us to base64 encoding. I found two code project articles to help me with zipping files with Sharpziplib and base64 encoding that would fit into what I needed.
The code
After having some discussion with my co-workers, we decided on an XML structure that looks like this:
<FILES>
<FILE FileName="file.txt"><CDATA></CDATA></File>
<FILE FileName="file2.txt"><CDATA></CDATA></File>
</FILES>
where we could add as many file nodes in the XML document as we wanted. Here is the code to create that XML doc:
public static XmlDocument GetFileXml(String[] Files)
{
XmlDocument tmpXmlDoc = new XmlDocument();
tmpXmlDoc.LoadXml("<FILES></FILES>");
for (Int32 i=0;i<=Files.Length-1;i++)
{
XmlElement tmpFile =
tmpXmlDoc.CreateElement("File");
XmlAttribute tmpA =
tmpXmlDoc.CreateAttribute("FileName");
tmpA.Value =
Files[i].Substring(Files[i].LastIndexOf(@"\")+1);
tmpFile.Attributes.Append(tmpA);
Byte[] myCompressed;
myCompressed =
CompressionHelper.CompressFile(Files[i]);
Char[] myEncoded;
Base64Encoder myBE;
myBE = new Base64Encoder(myCompressed);
myEncoded = myBE.GetEncoded();
StringBuilder myStr = new StringBuilder();
myStr.Append(myEncoded);
tmpFile.AppendChild(tmpXmlDoc.CreateCDataSection(
myStr.ToString()));
tmpXmlDoc.DocumentElement.AppendChild(tmpFile);
}
return tmpXmlDoc;
}
Next, we call the web service to get the XML document. Then, we parse through the XML file and extract and save the documents from the CDATA nodes.
public static void GetFiles(XmlNode inXml, String FilePath)
{
XmlNodeList myFile;
myFile = inXml.SelectNodes("/File");
for (Int32 j=0;j<=myFile.Count-1;j++)
{
XmlNode myCData = (XmlNode)(myFile.Item(j)).FirstChild;
String FileName =
((XmlNode)(myFile.Item(j))).Attributes.Item(0).InnerText;
Char[] theFile;
theFile = new Char[myCData.InnerText.Length];
for (Int32 i=0;i<=myCData.InnerText.Length-1;i++)
{
theFile[i] = myCData.InnerText[i];
}
if (File.Exists(FilePath+FileName))
{
File.Delete(FilePath+FileName);
}
Byte[] myDecoder;
Base64Decoder myBD = new Base64Decoder(theFile);
myDecoder = myBD.GetDecoded();
CompressionHelper.DecompressFile(myDecoder,
FilePath+FileName);
}
}
Conclusion
I would like to thank my co-worker Steve Rowe for his help and guidance in coming to this solution. I would also like to thank Uwe Keim for his shareziplib article and wchvic for his base64 article. Hopefully, others will find this article helpful in their solutions.
I started my programmer career over 26 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2019.
On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.