public static void DoWork() { //Sample XML var xml = CreateXml(); //File to write to var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf"); //Standard PDF creation, nothing special here using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (var doc = new Document()) { using (var writer = PdfWriter.GetInstance(doc, fs)) { doc.Open(); //Count the columns var columnCount = xml.Root.Elements("cd").First().Nodes().Count(); //Create a table with one column for every child node of <cd> var t = new PdfPTable(columnCount); //Flag that the first row should be repeated on each page break t.HeaderRows = 1; //Loop through the first item to output column headers foreach (var N in xml.Root.Elements("cd").First().Elements()) { t.AddCell(N.Name.ToString()); } //Loop through each CD row (this is so we can call complete later on) foreach (var CD in xml.Root.Elements()) { //Loop through each child of the current CD. Limit the number of children to our initial count just in case there are extra nodes. foreach (var N in CD.Elements().Take(columnCount)) { t.AddCell(N.Value); } //Just in case any rows have too few cells fill in any blanks t.CompleteRow(); } //Add the table to the document doc.Add(t); doc.Close(); Process.Start(testFile); } } } }
public static void TestPdfRepositoryV1() { GetXmlDataFromDatatBase(); //Sample XML //var xml = CreateXml(); #region -- ******************************* -- // https://ehikioya.com/get-xml-document-nodes-recursively/ XmlReader xmlFile; xmlFile = XmlReader.Create("..\\..\\Employees.xml", new XmlReaderSettings()); DataSet ds = new DataSet(); ds.ReadXml(xmlFile); int i = 0; for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { Console.WriteLine(" ===> " + ds.Tables[0].Rows[i].ItemArray[2].ToString()); } // - Search in XML -- DataView dv; dv = new DataView(ds.Tables[0]); //dv.Sort = "Product_Name"; //int index = dv.Find("Product2"); #endregion XElement xelement = XElement.Load("..\\..\\Employees.xml"); var employees = (from nm in xelement.Elements("Employee") select nm).ToList(); XDocument xml = XDocument.Load("..\\..\\Employees.xml"); foreach (XElement element in xml.Descendants()) { Console.WriteLine(element.Name); } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load("..\\..\\Employees.xml"); XmlNodeList xnList = xmlDocument.SelectNodes("/Employees/Employee"); foreach (XmlNode xn in xnList) { string id = xn["EmpId"].InnerText; string lastName = xn["Name"].InnerText; string sex = xn["EmpId"].InnerText; string phone = xn["Phone"].InnerText; Console.WriteLine("Employee : {0} {1} {2} {3}", id, lastName, sex, phone); } //File to write to var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf"); //Standard PDF creation, nothing special here using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (var doc = new Document()) { using (var writer = PdfWriter.GetInstance(doc, fs)) { doc.Open(); //Count the columns var columnCount = xml.Root.Elements("Employee").First().Nodes().Count(); //Create a table with one column for every child node of <cd> var t = new PdfPTable(columnCount); //Flag that the first row should be repeated on each page break t.HeaderRows = 1; //Loop through the first item to output column headers foreach (var N in xml.Root.Elements("Employee").First().Elements()) { t.AddCell(N.Name.ToString()); } //Loop through each CD row (this is so we can call complete later on) foreach (var CD in xml.Root.Elements()) { //Loop through each child of the current CD. Limit the number of children to our initial count just in case there are extra nodes. foreach (var N in CD.Elements().Take(columnCount)) { t.AddCell(N.Value); } //Just in case any rows have too few cells fill in any blanks t.CompleteRow(); } //Add the table to the document doc.Add(t); doc.Close(); Process.Start(testFile); } } } }