October 16, 2009

MyFax XML Web. Creating the XML file to be sent to MyFax.com




I needed to create a small prototype code for an application and send faxes from our interface using MyFax XML WebService. Here is only one of the steps: How to create the XML file to be sent to their WebService. I don't have any connection to a database but the intention is to send "n" amount of faxes created already in a database and send them automatically. Do not hesitate to contact me if you have any question/problem about this code.


    string CreateXMLFile(string base64PDF)
    {
        string strXMLFile = "";
        try 
        {
            XmlWriterSettings oSettings = new XmlWriterSettings();
            oSettings.Indent = true;
            oSettings.OmitXmlDeclaration = true;
            oSettings.Encoding = Encoding.UTF8;

            _myXMLFile = "MyFaxFile.xml";
            using (XmlWriter writer = XmlWriter.Create(MapPath(_myXMLFile), oSettings))
            {
                writer.WriteStartDocument(true);
                writer.WriteStartElement("single_fax_info", "http://www.protus.com");
                writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteStartElement("SchemaVersion");
                writer.WriteValue("1.2");
                writer.WriteEndElement();

                writer.WriteStartElement("login_key");
                writer.WriteStartElement("user_id");
                writer.WriteValue("12345"); //TO BE REPLACED WITH REAL VALUE
                writer.WriteEndElement();
                writer.WriteStartElement("user_password");
                writer.WriteValue("abcd123"); //TO BE REPLACED WITH REAL VALUE
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("single_fax_options");
                writer.WriteStartElement("billing_code");
                writer.WriteValue("cust 123"); //TO BE REPLACED WITH REAL VALUE
                writer.WriteEndElement();
                writer.WriteStartElement("from_name");
                writer.WriteValue(txtFrom.Text.ToString()); //TO BE REPLACED WITH VALUE FROM THE DATABASE
                writer.WriteEndElement();
                writer.WriteStartElement("tiff_image_flag");
                writer.WriteValue("false");
                writer.WriteEndElement();
                writer.WriteStartElement("resolution");
                writer.WriteValue("high");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("fax_recipient");
                writer.WriteStartElement("fax_recipient_number");
                writer.WriteValue(txtFaxNumber.Text.ToString()); //TO BE REPLACED WITH VALUE FROM THE DATABASE
                writer.WriteEndElement();
                writer.WriteStartElement("fax_recipient_name");
                writer.WriteValue(txtTo.Text.ToString()); //TO BE REPLACED WITH VALUE FROM THE DATABASE
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("document_list");
                //START OF ONE OR MANY PDFs TO BE SENT, YOU WOULD NEED TO CREATE A LOOP HERE
                writer.WriteStartElement("document");
                writer.WriteStartAttribute("document_content_type");
                writer.WriteValue("application/pdf");
                writer.WriteEndAttribute();
                writer.WriteStartAttribute("document_encoding_type");
                writer.WriteValue("base64");
                writer.WriteEndAttribute();
                writer.WriteStartAttribute("document_extension");
                writer.WriteValue("pdf");
                writer.WriteEndAttribute();
                writer.WriteValue(base64PDF); //TO BE REPLACED WITH THE PDF DOC CONVERTED TO BASE64
                writer.WriteEndElement();
                //END OF ONE OR MANY PDFs TO BE SENT
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(MapPath(_myXMLFile));
                StringWriter sWriter = new StringWriter();
                XmlTextWriter xWriter = new XmlTextWriter(sWriter);
                xmlDoc.WriteTo(xWriter);
                strXMLFile = sWriter.ToString();
            }
        }
        catch
        {
            throw;
        }
        return strXMLFile;
    }

Bookmark and Share