Advertisement
ChristerJohansson

XML Generator

Jun 19th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using System.Xml.Linq;
  6. using System.Xml.Serialization;
  7. using System.IO;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Xml.Schema;
  12. using HtmlAgilityPack;
  13.  
  14. namespace XML_Template_Generator
  15. {
  16.     class Program
  17.     {
  18.         public class Global
  19.         {
  20.             // vars
  21.             public static string AllFilesExtension = "*.*";
  22.             public static string HtmlExtension = "*.html";
  23.             public static string JpgExtension = "*.jpg";
  24.             public static string GifExtension = "*.gif";
  25.             public static string PngExtension = "*.png";
  26.             public static string ArchiveDirectory = @"D:\Journals\";
  27.             public static string Directory = @"D:\Journals\1-1";
  28.            
  29.         }
  30.  
  31.         public class AuthorData
  32.         {
  33.             public AuthorData(string firstName, string lastName, string email)
  34.             {
  35.                 FirstName = firstName;
  36.                 LastName = lastName;
  37.                 Email = email;
  38.             }
  39.             public static string FirstName { get; set; }
  40.             public static string LastName { get; set; }
  41.             public static string Email { get; set; }
  42.         }
  43.  
  44.         public class XmlGenerator
  45.         {
  46.             public static string[] htmlFilelist = Directory.GetFiles(Global.Directory, Global.HtmlExtension);
  47.  
  48.             // Issue and article specific strings
  49.             public static string Title = "";
  50.             public static string Description = "";
  51.             public static string AuthorsFirstname = AuthorData.FirstName;
  52.             public static string AuthorsLastname = AuthorData.LastName;
  53.             public static string AuthorEmail = AuthorData.Email;
  54.             public static string Publisher = "";
  55.             public static string PrimaryAuthorFirstname = "";
  56.             public static string PrimaryAuthorLastname = "";
  57.             public static string PrimaryAuthorEmail = "info@email.se";
  58.  
  59.             public static string XmlConstructor(){
  60.  
  61.                 // XML constructor
  62.                 XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
  63.                 XDocument xmldocument = new XDocument(
  64.                     new XDeclaration("1.0", "utf-8", "yes"),
  65.                     new XComment("Creating the issues and articles tree for import"),
  66.                     new XElement("issues",
  67.                         new XElement("issue",
  68.                             new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
  69.                             new XAttribute("published", true),
  70.                             new XAttribute("current", false),
  71.                             new XAttribute("access_status", "1"),
  72.                             new XAttribute(xsi + "schemaLocation", "http://dev.openjournal.tld native.xsd"),
  73.                             new XElement("id",
  74.                                 new XAttribute("type", "internal"),
  75.                                 new XAttribute("advice", "ignore"), "4"),
  76.                             new XElement("description",
  77.                                 new XAttribute("locale", "en_US"), Description),
  78.                             new XElement("issue_identification",
  79.                                 new XElement("volume", 1),
  80.                                 new XElement("number", 1),
  81.                                 new XElement("year", 1995),
  82.                                 new XElement("title", Title,
  83.                                     new XAttribute("locale", "en-us"))),
  84.                             new XElement("date_published", "2018-05-16"),
  85.                             new XElement("last_modified", "2018-05-16"),
  86.                             new XElement("sections",
  87.                                 new XElement("section",
  88.                                     new XAttribute("ref", "ART"),
  89.                                     new XAttribute("seq", 0),
  90.                                     new XAttribute("editor_restricted", 0),
  91.                                     new XAttribute("meta_indexed", 1),
  92.                                     new XAttribute("abstracts_not_required", 0),
  93.                                     new XAttribute("hide_title", 0),
  94.                                     new XAttribute("hide_author", 0),
  95.                                     new XAttribute("abstract_word_count", 0)),
  96.                                 new XElement("id",
  97.                                     new XAttribute("type", "internal"),
  98.                                     new XAttribute("advice", "ignore")),
  99.                                 new XElement("abbrev",
  100.                                     new XAttribute("locale", "en_US"), "ART"),
  101.                                 new XElement("title",
  102.                                     new XAttribute("locale", "en_US"), "Artiklar")),
  103.                             new XElement("issue_covers",
  104.                                 new XElement("cover",
  105.                                     new XAttribute("locale", "en_US")),
  106.                                 new XElement("cover_image", "cover_issue_4_en_US.jpg"),
  107.                                 new XElement("cover_image_alt_text")),
  108.                             new XElement("issue_galleys",
  109.                                 new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
  110.                                 new XAttribute(xsi + "schemaLocation", "http://dev.openjournal.tld native.xsd"),
  111.                                 new XElement("issue_galley",
  112.                                     new XAttribute("locale", "en_US"),
  113.                                     new XElement("label", "Paper1"),
  114.                                     from f in htmlFilelist
  115.                                     select new XElement("issue_file",
  116.                                         new XElement("file_name", f),
  117.                                         new XElement("file_type", "text/html"),
  118.                                         new XElement("file_size", "FILE SIZE"),
  119.                                         new XElement("file_size", "FILE SIZE"),
  120.                                         new XElement("content_type", 1),
  121.                                         new XElement("original_file_name", "FILE NAME"),
  122.                                         new XElement("date_uploaded", "2018-05-16"),
  123.                                         new XElement("date_modified", "2018-05-16")))),
  124.                             new XElement("articles",
  125.                                 new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
  126.                                 new XAttribute(xsi + "schemaLocation", "http://dev.openjournal.tld native.xsd"),
  127.                                 new XElement("article",
  128.                                     new XAttribute(XNamespace.Xmlns + "xsi",
  129.                                         "http://www.w3.org/2001/XMLSchema-instance"),
  130.                                     new XAttribute("locale", "en_US"),
  131.                                     new XAttribute("date_submitted", DateTime.Now.ToString("yyyy-MM-dd")),
  132.                                     new XAttribute("stage", "production"),
  133.                                     new XAttribute("date_published", "1995-01-01"),
  134.                                     new XAttribute("section_ref", "ART"),
  135.                                     new XAttribute("seq", 1),
  136.                                     new XAttribute("access_status", 0),
  137.                                     new XElement("id",
  138.                                         new XAttribute("type", "internal"),
  139.                                         new XAttribute("advice", "ignore"), 5),
  140.                                     new XElement("title",
  141.                                         new XAttribute("locale", "en_US"), Title),
  142.                                     new XElement("abstract",
  143.                                         new XAttribute("locale", "en_US"), Description),
  144.                                     new XElement("licenseUrl", "http://creativecommons.org/licenses/by-nc-nd/4.0"),
  145.                                     new XElement("copyrightHolder",
  146.                                         new XAttribute("locale", "en_US"), "INSERT NAME OF COPYRIGHT HOLDER HERE"),
  147.                                     new XElement("copyrightYear", "INSERT YEAR HERE"),
  148.                                     new XElement("keywords",
  149.                                         new XAttribute("locale", "en_US"),
  150.                                         new XElement("keyword", "HOW DO I GET MULTIPLE KEYWORDS?")),
  151.                                     new XElement("authors",
  152.                                         new XAttribute(XNamespace.Xmlns + "xsi",
  153.                                             "http://www.w3.org/2001/XMLSchema-instance"),
  154.                                         new XAttribute(xsi + "schemaLocation", "http://dev.openjournal.tld native.xsd"),
  155.                                         new XElement("author",
  156.                                             new XAttribute("primary_contact", "true"),
  157.                                             new XAttribute("include_in_browser", "true"),
  158.                                             new XAttribute("user_group_ref", "Authors"),
  159.                                             new XElement("firstname", PrimaryAuthorFirstname),
  160.                                             new XElement("lastname", PrimaryAuthorLastname),
  161.                                             new XElement("email", PrimaryAuthorEmail)),
  162.                                         new XElement("author",
  163.                                             new XAttribute("primary_contact", "false"),
  164.                                             new XAttribute("include_in_browser", "true"),
  165.                                             new XAttribute("user_group_ref", "Authors"),
  166.                                             new XElement("firstname", AuthorData.FirstName),
  167.                                             new XElement("lastname", AuthorData.LastName),
  168.                                             new XElement("email", AuthorData.Email))),
  169.                                     new XElement("submission_file",
  170.                                         new XAttribute(XNamespace.Xmlns + "xsi",
  171.                                             "http://www.w3.org/2001/XMLSchema-instance"),
  172.                                         new XAttribute("id", "INSERT ID HERE"),
  173.                                         new XAttribute(xsi + "schemaLocation", "http://dev.openjournal.tld native.xsd"),
  174.                                         new XElement("revision",
  175.                                             new XAttribute("number", "1"),
  176.                                             new XAttribute("genre", "ARTIKELTEXT"),
  177.                                             new XAttribute("filename", "INSERT FILENAME HERE"),
  178.                                             new XAttribute("date_uploaded", "INSERT DATE FOR UPLOAD HERE"),
  179.                                             new XAttribute("date_modified", "INSERT DATE FOR LAST MODIFICATION HERE"),
  180.                                             new XAttribute("filesize", "INSERT FILE SIZE HERE (MIGHT BE OPTIONAL)"),
  181.                                             new XAttribute("filetype", "INSERT TYPE OF FILE text/html OR PICTURES"),
  182.                                             new XAttribute("user_group", "Authors"),
  183.                                             new XAttribute("uploader", "INSERT FIRST AUTHOR AS UPLOADER HERE"),
  184.                                             new XElement("name",
  185.                                                 new XAttribute("locale", "en_US"), "AUTHORSNAME, USER_GROUP, FILENAME"),
  186.                                             new XElement("href",
  187.                                                 new XAttribute("src", "http://localhost/importfolder/papers.html"),
  188.                                                 new XAttribute("mime_type", "text/url"))
  189.                                         )))))));
  190.                 return xmldocument.ToString();
  191.             }
  192.         }
  193.         static void Main(string[] args)
  194.         {
  195.             // list all directory paths. Use in a foreach-loop to select each directory.
  196.             string[] dirs = Directory.GetDirectories(Global.ArchiveDirectory, Global.AllFilesExtension, SearchOption.TopDirectoryOnly);
  197.  
  198.             // List all files and types in invidual lists
  199.             string[] htmlFilelist = XmlGenerator.htmlFilelist;
  200.             var jpgFilelist = Getfile(Global.Directory, Global.JpgExtension);
  201.             var gifFilelist = Getfile(Global.Directory, Global.GifExtension);
  202.             var pngFilelist = Getfile(Global.Directory, Global.PngExtension);
  203.  
  204.             // Read every file in list and find information with XPath
  205.             foreach (string file in htmlFilelist)
  206.             {
  207.                 HtmlDocument doc = new HtmlDocument();
  208.                 doc.Load(file);
  209.                 HtmlNodeNavigator navigator = (HtmlNodeNavigator) doc.CreateNavigator();
  210.  
  211.                 // Get the title for the file
  212.                 string title_xpath = "//meta[@name='DC.Title']/@content";
  213.                 XmlGenerator.Title = navigator.SelectSingleNode(title_xpath).Value;
  214.  
  215.                 // Get the description of the document
  216.                 string description_xpath = "//meta[@name='DC.Description']/@content";
  217.                 XmlGenerator.Description = navigator.SelectSingleNode(description_xpath).Value;
  218.  
  219.                 // Get the author or authors in to a list
  220.                 string author_xpath = "//meta[@name='DC.Creator']/@content";
  221.                 var authors = new List<string>();
  222.                 string[] author = navigator.SelectSingleNode(author_xpath).Value.Split(',');
  223.                 foreach (var a in author)
  224.                 {
  225.                     authors.Add(a);
  226.                 }
  227.  
  228.                 // Split their names into first and lastname
  229.                 var authorName = authors.First().Split(' ');
  230.  
  231.                 // Get the publisher meta tag
  232.                 string publisher_xpath = "//meta[@name='DC.Publisher']/@content";
  233.                 XmlGenerator.Publisher = navigator.SelectSingleNode(publisher_xpath).Value;
  234.  
  235.                 // Test for co-authors names
  236.                 AuthorData.FirstName = "Kalle";
  237.                 AuthorData.LastName = "Andersson";
  238.                 AuthorData.Email = "authors@email.com";
  239.  
  240.                 // Test with output to console
  241.                 Console.WriteLine("*** Authors ***");
  242.                 foreach (var a in authors)
  243.                 {
  244.                     Console.WriteLine(a);
  245.                 }
  246.                 Console.WriteLine("File: {0}", file);
  247.                 Console.WriteLine("Title: {0}", XmlGenerator.Title);
  248.                 Console.WriteLine();
  249.                 Console.WriteLine("Description: {0}", XmlGenerator.Description);
  250.                 Console.WriteLine();
  251.                 Console.WriteLine("Primary Author: {0} {1}", authorName[0], authorName[1]);
  252.                 Console.WriteLine("Primary Author Email: {0}", XmlGenerator.PrimaryAuthorEmail);
  253.                 Console.WriteLine("Publisher: {0}", XmlGenerator.Publisher);
  254.                 Console.WriteLine("********");
  255.                 }
  256.            
  257.             // Output to console for testing
  258.             Console.WriteLine("Total number of directories: {0}", dirs.Count());
  259.             Console.WriteLine("Number of html-files: {0}", htmlFilelist.Count());
  260.             Console.WriteLine("Number of jpg-files: {0}", jpgFilelist.Count());
  261.             Console.WriteLine("Number of gif-files: {0}", gifFilelist.Count());
  262.             Console.WriteLine("Number of png-files: {0}", pngFilelist.Count());
  263.             Console.WriteLine("*** XML ***");
  264.             Console.WriteLine(XmlGenerator.XmlConstructor());
  265.             Console.ReadKey();
  266.         }
  267.         private static IEnumerable<string> Getfile(string fdir, string ext)
  268.         {
  269.             List<string> files = new List<string>();
  270.             try
  271.             {
  272.                 files.AddRange(Directory.GetFiles(fdir, ext, SearchOption.AllDirectories));
  273.             }
  274.             catch (Exception ex)
  275.             {
  276.                 Console.WriteLine(ex.Message);
  277.             }
  278.             return files;
  279.         }
  280.         static List<string> build_directory_list(string dir)
  281.         {
  282.             // Get all subdirectories
  283.             string[] subdirectories = Directory.GetDirectories(dir);
  284.             List<string> directories = new List<string>();
  285.             foreach (string directory in subdirectories)
  286.             {
  287.                 directories.Add(directory);
  288.             }
  289.             return directories;
  290.         }
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement