public static void SaveContentTypes(XFile file, MemoryFolder mFolder, ExcelFileType workbookType) { CT_Types types = GetContentTypes(file, mFolder, workbookType); if (types != null) { mFolder.CreateMemoryFile("[Content_Types].xml", CreateStreamFromObject(types, typeof(CT_Types))); } }
public static Stream CreateStreamFromObject(object obj, Type type) { if (obj == null) { return(null); } Stream stream = null; if (obj is CT_Relationships) { CT_Relationships relationships = obj as CT_Relationships; stream = (Stream) new MemoryStream(); new XmlWriterSettings().Encoding = Encoding.UTF8; XmlWriter @this = XmlWriter.Create(stream, new XmlWriterSettings()); using (@this.WriteDocument(true)) { using (@this.WriteElement("Relationships", "http://schemas.openxmlformats.org/package/2006/relationships")) { foreach (Relationship relationship in relationships.Relationship) { using (@this.WriteElement("Relationship")) { @this.WriteAttributeString("Id", relationship.Id); @this.WriteAttributeString("Type", relationship.Type); @this.WriteAttributeString("Target", relationship.Target.Trim(new char[] { '\n', '\b', ' ' })); if (!string.IsNullOrWhiteSpace(relationship.TargetMode)) { @this.WriteAttributeString("TargetMode", relationship.TargetMode); } } } } } @this.Flush(); stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin); return(stream); } if (obj is CT_Types) { CT_Types types = obj as CT_Types; stream = (Stream) new MemoryStream(); new XmlWriterSettings().Encoding = Encoding.UTF8; XmlWriter writer2 = XmlWriter.Create(stream, new XmlWriterSettings()); using (writer2.WriteDocument(true)) { using (writer2.WriteElement("Types", "http://schemas.openxmlformats.org/package/2006/content-types")) { foreach (object obj2 in types.Items) { if (obj2 is CT_Default) { CT_Default default2 = obj2 as CT_Default; using (writer2.WriteElement("Default")) { writer2.WriteAttributeString("ContentType", default2.ContentType); writer2.WriteAttributeString("Extension", default2.Extension); continue; } } if (obj2 is CT_Override) { CT_Override @override = obj2 as CT_Override; using (writer2.WriteElement("Override")) { writer2.WriteAttributeString("ContentType", @override.ContentType); writer2.WriteAttributeString("PartName", @override.PartName); } } } } } writer2.Flush(); stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin); } return(stream); }