public static void writeContentTypes(string ext) { try { XMLdata data = getRelsPath(ext); string path = data.contentTypesPath; //if (!File.Exists(path)) //{ // XmlTextWriter writer = new XmlTextWriter(path, null); // writer.WriteStartElement("Relationships"); // writer.WriteEndElement(); // writer.Close(); //} XNamespace xmlns = XNamespace.Get("http://schemas.openxmlformats.org/package/2006/content-types"); XElement xml = XElement.Load(path); xml.Add( new XElement(xmlns + "Override", new XAttribute("PartName", data.partName), new XAttribute("ContentType", data.contentType) )); xml.Save(path); } catch (Exception ex) { LogService.add(ex.ToString()); } }
static XMLdata getRelsPath(string ext) { XMLdata data = new XMLdata(); data.contentTypesPath = @"temp\[Content_Types].xml"; if (ext == "*.docx") { data.relsPath = "temp\\word\\_rels\\document.xml.rels"; //do poprawy - sprawdzić ścieżkę w pliku docx. data.relType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom"; data.partName = "/word/temp.xml"; data.contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.temp+xml"; } if (ext == "*.xlsx") { data.relsPath = "temp\\xl\\_rels\\workbook.xml.rels"; data.relType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"; } else if (ext == "*.pptx") { data.relsPath = "temp\\ppt\\_rels\\presentation.xml.rels"; data.relType = "http://schemas.openxmlformats.org/package/2006/relationships"; } return(data); }
/// <summary> /// Writes new relationshio data to ~/_rels/document.xml.rels file. /// This operation is required to avoid deletion of encrypted file after OOXML file modification. /// </summary> public static void writeRels(string ext) { try { XMLdata data = getRelsPath(ext); string path = data.relsPath; string type = data.relType; if (!File.Exists(path)) { XmlTextWriter writer = new XmlTextWriter(path, null); writer.WriteStartElement("Relationships"); writer.WriteEndElement(); writer.Close(); } XNamespace xmlns = XNamespace.Get("http://schemas.openxmlformats.org/package/2006/relationships"); XElement xml = XElement.Load(path); xml.Add( new XElement(xmlns + "Relationship", new XAttribute("Id", "rId99"), new XAttribute("Type", type), new XAttribute("Target", "temp.xml")) ); xml.Save(path); } catch (Exception ex) { LogService.add(ex.ToString()); } }