public void Process(IntPtr hFile, string sFile, ISXMLElement pXML) { bool success = true; try { success = (Path.GetExtension(sFile) == @".fb2") && hFile != IntPtr.Zero; } catch { } if (!success) { Marshal.ThrowExceptionForHR(Hresults.E_INVALIDARG); } XmlReader reader = null; FictionBook book = null; Stream fs = null; try { XmlSerializer s = new XmlSerializer(typeof(FictionBook)); s.UnknownNode += new XmlNodeEventHandler(OnUnknownNode); s.UnknownAttribute += new XmlAttributeEventHandler(OnUnknownAttribute); fs = new FileStream(sFile, FileMode.Open, FileAccess.Read); reader = new XmlTextReader(fs); book = (FictionBook)s.Deserialize(reader); } catch (Exception e) { Trace.WriteLine(e.Message); Exception inner = e.InnerException; while (inner != null) { Trace.WriteLine(inner.Message); inner = inner.InnerException; } } finally { if (reader != null) { reader.Close(); } if (fs != null) { fs.Close(); } } if (book == null) { Marshal.ThrowExceptionForHR(Hresults.E_FAIL); } int result = Hresults.S_OK; try { CreateEnvyXml(book, ref pXML); } catch (Exception e) { Trace.WriteLine(e.Message); Exception inner = e.InnerException; while (inner != null) { Trace.WriteLine(inner.Message); inner = inner.InnerException; } result = Marshal.GetHRForException(e); } if (result != 0) { Trace.WriteLine("Error HRESULT=" + result.ToString()); Marshal.ThrowExceptionForHR(result); } }
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); } } }
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); } } }
public void Process(IntPtr hFile, string sFile, ISXMLElement pXML) { bool success = true; try { success = (Path.GetExtension(sFile) == @".fb2") && hFile != IntPtr.Zero; } catch { } if (!success) Marshal.ThrowExceptionForHR(Hresults.E_INVALIDARG); XmlReader reader = null; FictionBook book = null; Stream fs = null; try { XmlSerializer s = new XmlSerializer(typeof(FictionBook)); s.UnknownNode += new XmlNodeEventHandler(OnUnknownNode); s.UnknownAttribute += new XmlAttributeEventHandler(OnUnknownAttribute); fs = new FileStream(sFile, FileMode.Open, FileAccess.Read); reader = new XmlTextReader(fs); book = (FictionBook)s.Deserialize(reader); } catch (Exception e) { Trace.WriteLine(e.Message); Exception inner = e.InnerException; while (inner != null) { Trace.WriteLine(inner.Message); inner = inner.InnerException; } } finally { if (reader != null) reader.Close(); if (fs != null) fs.Close(); } if (book == null) { Marshal.ThrowExceptionForHR(Hresults.E_FAIL); } int result = Hresults.S_OK; try { CreateShareazaXml(book, ref pXML); } catch (Exception e) { Trace.WriteLine(e.Message); Exception inner = e.InnerException; while (inner != null) { Trace.WriteLine(inner.Message); inner = inner.InnerException; } result = Marshal.GetHRForException(e); } if (result != 0) { Trace.WriteLine("Error HRESULT=" + result.ToString()); Marshal.ThrowExceptionForHR(result); } }