/// <summary> /// Deserializes a html document /// </summary> /// <param name="documentHtml">The document HTML</param> /// <param name="documentText">The document text</param> /// <param name="documentXml">The document text</param> /// <param name="displayHtml">(optional) The display HTML</param> /// <returns> /// the html document /// </returns> /// <exception cref="System.Runtime.Serialization.SerializationException">Error deserializing html document</exception> public virtual SerializableDocument Deserialize(string documentHtml, string documentText, string documentXml, string displayHtml = null) { try { SerializableElementList elementArray; using (var stringReader = new StringReader(documentXml)) using (var xmlReader = XmlReader.Create(stringReader, _readerSettings)) { elementArray = (SerializableElementList)_serializer.Deserialize(xmlReader); } var elements = elementArray.SerializableElements; var elementLookup = SerializationUtils.CreateLookup(elements); foreach (var element in elements) { element.Link(elementLookup); element.CropHtmlAndText(documentHtml, documentText); element.SetDefaultStyleLookup(_defaultStyleLookup); } var info = new HtmlDocumentInfo(elementArray.Url, elementArray.BrowserVersion, elementArray.CodeVersion, elementArray.CreationDate); var doc = new SerializableDocument(elements[0], info, _defaultStyleLookup, documentHtml, documentText); doc.DisplayHtml = displayHtml; return(doc); } catch (Exception ex) { throw new SerializationException("Error deserializing html document", ex); } }
/// <summary> /// Serializes a serializable document /// </summary> /// <param name="document">The serializable document</param> /// <returns>Document XML</returns> private string Serialize(SerializableDocument document) { var orderedElements = document.All.OrderBy(n => n.Id).ToArray().ConvertAll <SerializableElement>(); var toSerialize = new SerializableElementList(orderedElements, document.Info); using (var stringWriter = new StringWriter()) using (var xmlWriter = XmlWriter.Create(stringWriter, _writerSettings)) { _serializer.Serialize(xmlWriter, toSerialize, _namespaces); return(stringWriter.ToString()); } }