Adding a Infopath form Programmaticaly from a webpart

Adding a Infopath form programmaticaly seems like a piece of cake by following method:

First, add a blank form in the library, which we'll access from the code/web part, edit as a copy and add to the library.

private void button1_Click(object sender, EventArgs e)
            SPSite site = new SPSite("http://Site/SubSite");
            SPWeb web = site.OpenWeb();
            string strURL = ((SPDocumentLibrary)(web.Lists["Forms"])).DocumentTemplateUrl;
            // Gets the blank file to copy
            SPFile BLANK = web.Folders["invoices"].Files["master.xml"];
            // Reads the blank file into an xml document
            MemoryStream inStream = new MemoryStream(BLANK.OpenBinary());
            XmlTextReader reader = new XmlTextReader(inStream);
            XmlDocument xd = new XmlDocument();

            //Get the XML Document
            XmlNamespaceManager nm = new XmlNamespaceManager(xd.NameTable);
            nm.AddNamespace("my", "");
            XmlNode node = xd.SelectSingleNode("//my:Title",nm);
            OperateNode(ref node); 

            // saves the XML Document back as a file
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            SPFile newFile = web.Folders["Forms"].Files.Add("Test_Document.xml", (encoding.GetBytes(xd.OuterXml)), true);

In this way, a new form with custom node values will be submitted to the Infopath library. The above method is very much dependent on the BLANK.XML form in the library, hence it will be good idea to hide the blank form from the default view.