public HtmlDocumentFacade(XDocument document) { this.document = document; html = document.CreateElement("html"); document.AppendChild(html); body = document.CreateElement("body"); head = document.CreateElement("head"); stylesheetElement = document.CreateElement("style"); stylesheetElement.SetAttributeValue("type", "text/css"); html.AppendChild(head); html.AppendChild(body); head.AppendChild(stylesheetElement); AddCharset(); AddStyleClass(body, "b", "white-space-collapsing:preserve;"); }
/** * Exports the data in an XML stream * * @param os OutputStream in which will contain the output XML * @param encoding the output charset encoding * @param validate if true, validates the XML againts the XML Schema * @throws SAXException * @throws ParserConfigurationException * @throws TransformerException * @throws InvalidFormatException */ public void ExportToXML(Stream os, String encoding, bool validate) { List <XSSFSingleXmlCell> SingleXMLCells = map.GetRelatedSingleXMLCell(); List <XSSFTable> tables = map.GetRelatedTables(); String rootElement = map.GetCTMap().RootElement; XDocument doc = GetEmptyDocument(); XElement root = null; if (IsNamespaceDeclared()) { root = new XElement((XNamespace)this.GetNamespace() + rootElement); } else { root = doc.CreateElement(rootElement); } doc.AppendChild(root); List <String> xpaths = new List <String>(); Dictionary <String, XSSFSingleXmlCell> SingleXmlCellsMappings = new Dictionary <String, XSSFSingleXmlCell>(); Dictionary <String, XSSFTable> tableMappings = new Dictionary <String, XSSFTable>(); foreach (XSSFSingleXmlCell simpleXmlCell in SingleXMLCells) { xpaths.Add(simpleXmlCell.GetXpath()); SingleXmlCellsMappings[simpleXmlCell.GetXpath()] = simpleXmlCell; } foreach (XSSFTable table in tables) { String commonXPath = table.GetCommonXpath(); xpaths.Add(commonXPath); tableMappings[commonXPath] = table; } xpaths.Sort(); foreach (String xpath in xpaths) { XSSFSingleXmlCell simpleXmlCell; if (SingleXmlCellsMappings.ContainsKey(xpath)) { simpleXmlCell = SingleXmlCellsMappings[xpath]; } else { simpleXmlCell = null; } XSSFTable table; if (tableMappings.ContainsKey(xpath)) { table = tableMappings[xpath]; } else { table = null; } if (!Regex.IsMatch(xpath, ".*\\[.*")) { // Exports elements and attributes mapped with simpleXmlCell if (simpleXmlCell != null) { XSSFCell cell = (XSSFCell)simpleXmlCell.GetReferencedCell(); if (cell != null) { XElement currentNode = GetNodeByXPath(xpath, doc.Root.FirstNode as XElement, doc, false); ST_XmlDataType dataType = simpleXmlCell.GetXmlDataType(); mapCellOnNode(cell, currentNode, dataType); } } // Exports elements and attributes mapped with tables if (table != null) { List <XSSFXmlColumnPr> tableColumns = table.GetXmlColumnPrs(); XSSFSheet sheet = table.GetXSSFSheet(); int startRow = table.GetStartCellReference().Row; // In mappings Created with Microsoft Excel the first row Contains the table header and must be Skipped startRow += 1; int endRow = table.GetEndCellReference().Row; for (int i = startRow; i <= endRow; i++) { XSSFRow row = (XSSFRow)sheet.GetRow(i); XElement tableRootNode = GetNodeByXPath(table.GetCommonXpath(), doc.Root.FirstNode as XElement, doc, true); short startColumnIndex = table.GetStartCellReference().Col; for (int j = startColumnIndex; j <= table.GetEndCellReference().Col; j++) { XSSFCell cell = (XSSFCell)row.GetCell(j); if (cell != null) { XSSFXmlColumnPr pointer = tableColumns[j - startColumnIndex]; String localXPath = pointer.GetLocalXPath(); XElement currentNode = GetNodeByXPath(localXPath, tableRootNode, doc, false); ST_XmlDataType dataType = pointer.GetXmlDataType(); mapCellOnNode(cell, currentNode, dataType); } } } } } else { // TODO: implement filtering management in xpath } } bool isValid = true; if (validate) { isValid = this.IsValid(doc); } if (isValid) { ///////////////// //Output the XML XmlWriterSettings settings = new XmlWriterSettings(); //settings.OmitXmlDeclaration=false; settings.Indent = true; settings.Encoding = Encoding.GetEncoding(encoding); //create string from xml tree using (XmlWriter xmlWriter = XmlWriter.Create(os, settings)) { doc.WriteTo(xmlWriter); } } }