void CreateEnvyXml(FictionBook book, ref ISXMLElement pXML) { EnvyBook sbook = new EnvyBook(book); Books sBooks = new Books(); sBooks.books[0] = sbook; MemoryStream ms = null; XmlWriter writer = null; string finalXml = String.Empty; try { XmlSerializer s = new XmlSerializer(typeof(Books)); s.UnknownNode += new XmlNodeEventHandler(OnUnknownNode); s.UnknownAttribute += new XmlAttributeEventHandler(OnUnknownAttribute); ms = new MemoryStream(); UTF8Encoding utf8 = new UTF8Encoding(false, false); writer = new XmlTextWriter(ms, utf8); XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); // Don't add any prefixes xsn.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); s.Serialize(writer, sBooks, xsn); // Start modifying the resulting XML XmlDocument doc = new XmlDocument(); ms.Position = 0; doc.Load(ms); XmlAttribute schema = doc.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); schema.Value = Books.URI; doc.DocumentElement.SetAttributeNode(schema); // Truncate the serialization result and overwrite it with our modified XML ms.SetLength(0); ms.Position = 0; writer = new XmlTextWriter(ms, utf8); doc.Save(writer); char[] buffer = Encoding.UTF8.GetChars(ms.ToArray()); finalXml = new string(buffer); } catch (Exception e) { Trace.WriteLine(e.Message); Exception inner = e.InnerException; while (inner != null) { Trace.WriteLine(inner.Message); inner = inner.InnerException; } } finally { if (writer != null) { writer.Close(); } if (ms != null) { ms.Close(); } } if (!String.IsNullOrEmpty(finalXml)) { Trace.WriteLine(finalXml); ISXMLElement newXML = pXML.FromString(finalXml); if (newXML != null) { pXML.Elements.Attach(newXML); } } }
void CreateShareazaXml(FictionBook book, ref ISXMLElement pXML) { ShareazaBook sbook = new ShareazaBook(book); Books sBooks = new Books(); sBooks.books[0] = sbook; MemoryStream ms = null; XmlWriter writer = null; string finalXml = String.Empty; try { XmlSerializer s = new XmlSerializer(typeof(Books)); s.UnknownNode += new XmlNodeEventHandler(OnUnknownNode); s.UnknownAttribute += new XmlAttributeEventHandler(OnUnknownAttribute); ms = new MemoryStream(); UTF8Encoding utf8 = new UTF8Encoding(false, false); writer = new XmlTextWriter(ms, utf8); XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); // Don't add any prefixes xsn.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); s.Serialize(writer, sBooks, xsn); // Start modifying the resulting XML XmlDocument doc = new XmlDocument(); ms.Position = 0; doc.Load(ms); XmlAttribute schema = doc.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); schema.Value = Books.URI; doc.DocumentElement.SetAttributeNode(schema); // Truncate the serialization result and overwrite it with our modified XML ms.SetLength(0); ms.Position = 0; writer = new XmlTextWriter(ms, utf8); doc.Save(writer); char[] buffer = Encoding.UTF8.GetChars(ms.ToArray()); finalXml = new string(buffer); } catch (Exception e) { Trace.WriteLine(e.Message); Exception inner = e.InnerException; while (inner != null) { Trace.WriteLine(inner.Message); inner = inner.InnerException; } } finally { if (writer != null) writer.Close(); if (ms != null) ms.Close(); } if (!String.IsNullOrEmpty(finalXml)) { Trace.WriteLine(finalXml); ISXMLElement newXML = pXML.FromString(finalXml); if (newXML != null) { pXML.Elements.Attach(newXML); } } }