public string CleanUP_OnixElements(FileInfo file, string Company) //, string fullfilepath_Sortfile) { try { if (file.Exists) { using (StreamReader Reader = new StreamReader(file.FullName)) { StringBuilder Sb1 = new StringBuilder(); StringBuilder Sb2 = new StringBuilder(); string line = ""; #region 'case 2' int filesize = (int)file.Length; string declaration = ""; string xml_Declaration = ""; //50000000 //if (filesize > 10) if (filesize > 50000000) { #region '' using (StreamReader sr = new StreamReader(file.FullName)) { var xDoc = XDocument.Parse(sr.ReadToEnd()); // loading source xml xml_Declaration = xDoc.Declaration.ToString(); //string asdf string rootName = xDoc.Root.Name.ToString(); string firstnode = xDoc.FirstNode.ToString(); //Console.WriteLine("First Node: " + firstnode); //Console.WriteLine("Root Node: " + rootName); //string asdf = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"; var xmls = xDoc.Root.Elements().ToArray(); // split into elements #region 'Merge' // harlequin_xml20749 -- xmls.Length string xmltext = ""; using (StreamWriter sw1 = new StreamWriter(cleanedupFilePath + file.Name)) { sw1.WriteLine(declaration); if (rootName.Contains("ONIXMessage")) { sw1.WriteLine("<ONIXMessage>"); } else { sw1.WriteLine("<ONIXmessage>"); } for (int i = 0; i < xmls.Length; i++) { xmltext = ReferenceTags_ShortTags_Translation(SanitizeXmlString(xmls[i].ToString())); sw1.WriteLine(xmltext); } // sw1.WriteLine("</ONIXmessage>"); if (rootName.Contains("ONIXMessage")) { sw1.WriteLine("</ONIXMessage>"); } else { sw1.WriteLine("</ONIXmessage>"); } } #endregion #region 'Sort' //rootNode = XElement.Load(cleanedupFilePath + file.Name); //rootNode.Save(cleanedupFilePath + file.Name); #endregion } #endregion } else { #region '' Sb1.Append(Reader.ReadToEnd()); Sb1.Replace("xmlns=\"http://www.editeur.org/onix/2.1/reference\"", ""); Sb1.Replace("xmlns=\"http://www.editeur.org/onix/2.1/short\"", ""); rootNode = XElement.Parse(ReferenceTags_ShortTags_Translation(SanitizeXmlString(Sb1.ToString()))); //SortFile(onix_tagtype); rootNode.Save(cleanedupFilePath + file.Name); //declaration = @"<?xml version=""1.0"" encoding=""" + xml_Declaration + @"""?>"; #endregion } // SortFile(); #endregion } } else { throw new FileNotFoundException(file.FullName); } } catch (Exception ex) { CommonFunctions.SQLFunction sqlfunction = new CommonFunctions.SQLFunction(); sqlfunction.Insert_ErrorLog(sqlfunction.GetConnectionString(Company), ex.ToString()); //Console.WriteLine(ex.Message.ToString()); //Console.WriteLine(ex.GetBaseException().ToString()); //// Console.ReadLine(); ////CommonFunctions ForEmail = new CommonFunctions(); ////ForEmail.SendExceptionsInMail("Move Files And Add FileNames Validation - Exception", "<b>Error at Sort_OnixElements</b> " + file.FullName + "<br/>" + ex.Message, "*****@*****.**", "", ""); } return(cleanedupFilePath + file.Name); }
public string CleanUP_LargeXMLFiles(FileInfo file, string Company) //, string fullfilepath_Sortfile) { try { if (file.Exists) { #region 'case 2' int filesize = (int)file.Length; //50000000 //if (filesize > 10) //if (filesize > 100000000) //{ #region '' string xmltext = ""; using (StreamWriter sw1 = new StreamWriter(cleanedupFilePath + file.Name)) { using (StreamReader sr = new StreamReader(file.FullName)) { while (sr.Peek() >= 0) { //Console.WriteLine(sr.ReadLine()); sw1.WriteLine(ReferenceTags_ShortTags_Translation(SanitizeXmlString(sr.ReadLine()))); } } } #endregion #region 'Sort - commented' //rootNode = XElement.Load(cleanedupFilePath + file.Name); //rootNode.Save(cleanedupFilePath + file.Name); //} //else //{ // #region '' // using (StreamReader Reader = new StreamReader(file.FullName)) // { // StringBuilder Sb1 = new StringBuilder(); // StringBuilder Sb2 = new StringBuilder(); // string line = ""; // Sb1.Append(Reader.ReadToEnd()); // Sb1.Replace("xmlns=\"http://www.editeur.org/onix/2.1/reference\"", ""); // Sb1.Replace("xmlns=\"http://www.editeur.org/onix/2.1/short\"", ""); // rootNode = XElement.Parse(ReferenceTags_ShortTags_Translation(SanitizeXmlString(Sb1.ToString()))); // //SortFile(onix_tagtype); // rootNode.Save(cleanedupFilePath + file.Name); // //declaration = @"<?xml version=""1.0"" encoding=""" + xml_Declaration + @"""?>"; // } // #endregion //} // SortFile(); #endregion #endregion } else { throw new FileNotFoundException(file.FullName); } } catch (Exception ex) { CommonFunctions.SQLFunction sqlfunction = new CommonFunctions.SQLFunction(); sqlfunction.Insert_ErrorLog(sqlfunction.GetConnectionString(Company), ex.ToString()); //Console.WriteLine(ex.Message.ToString()); //Console.WriteLine(ex.GetBaseException().ToString()); //// Console.ReadLine(); ////CommonFunctions ForEmail = new CommonFunctions(); ////ForEmail.SendExceptionsInMail("Move Files And Add FileNames Validation - Exception", "<b>Error at Sort_OnixElements</b> " + file.FullName + "<br/>" + ex.Message, "*****@*****.**", "", ""); } return(cleanedupFilePath + file.Name); }
public ONIXMessage Work_With_Onix2referenceTags(string filelocation, System.Windows.Forms.Label lbl_Message, System.Windows.Forms.Label lbl_CleanUp, string Company, string MediaType, string FileName, string PublisherName, string OnixVersion, string TagType, string XML_Encoding) { lbl_CleanUp.BackColor = System.Drawing.Color.Yellow; lbl_CleanUp.Refresh(); System.Windows.Forms.Application.DoEvents(); #region 'CleanUp ONIX elements' ONIXMessage fileInfo_2reference = new ONIXMessage(); //System.IO.FileInfo file_1 = new System.IO.FileInfo(@"C:\Users\kkoka\Desktop\wfh\harpercollins\HCUK_ONIX_full_20141107.xml"); System.IO.FileInfo file_1 = new System.IO.FileInfo(filelocation); string CleanedFile_WithName = ""; try { // System.IO.FileInfo file_1 = new System.IO.FileInfo(file); Onix_CleanUP cleanup_xml = new Onix_CleanUP(); //(info, sort_filepath, onix_TagType , xml_encoding); CleanedFile_WithName = cleanup_xml.CleanUP_LargeXMLFiles(file_1, Company); //string CleanedFile_WithName = cleanup_xml.CleanUP_OnixElements(file_1, Company); // Create an instance of stream writer. // TextReader txtReader = new StreamReader(filelocation); TextReader txtReader = new StreamReader(CleanedFile_WithName, Encoding.GetEncoding(XML_Encoding)); // Create and instance of XmlSerializer class. XmlSerializer xmlSerializer = new XmlSerializer(typeof(ONIXMessage)); // DeSerialize from the StreamReader fileInfo_2reference = (ONIXMessage)xmlSerializer.Deserialize(txtReader); // Close the stream reader txtReader.Close(); lbl_CleanUp.BackColor = System.Drawing.Color.Green; lbl_CleanUp.Refresh(); System.Windows.Forms.Application.DoEvents(); } catch (Exception ex) { lbl_Message.Text = "There has been some problem cleaning up the file."; lbl_Message.Refresh(); System.Windows.Forms.Application.DoEvents(); lbl_CleanUp.BackColor = System.Drawing.Color.Red; lbl_CleanUp.Refresh(); System.Windows.Forms.Application.DoEvents(); CommonFunctions.SQLFunction sqlfnction = new CommonFunctions.SQLFunction(); sqlfnction.Insert_ErrorLog(sqlfnction.GetConnectionString(Company), "Error Cleaning/Deserializing the " + MediaType + " file : " + FileName + " from the publisher - " + PublisherName + ". Exception : " + ex.ToString()); } finally { File.Delete(CleanedFile_WithName); } return(fileInfo_2reference); #endregion }