// --------------------------------------------------------------------- // // Internal Methods // // --------------------------------------------------------------------- #region Internal Methods /// <summary> /// Converts an html string into xaml string. /// </summary> /// <param name="htmlString"> /// Input html which may be badly formated xml. /// </param> /// <param name="asFlowDocument"> /// true indicates that we need a FlowDocument as a root element; /// false means that Section or Span elements will be used /// dependeing on StartFragment/EndFragment comments locations. /// </param> /// <returns> /// Well-formed xml representing XAML equivalent for the input html string. /// </returns> public static string ConvertHtmlToXaml(string htmlString, bool asFlowDocument) { // setup SGMLReader SgmlReader sgmlReader = new SgmlReader(); sgmlReader.DocType = "HTML"; sgmlReader.WhitespaceHandling = WhitespaceHandling.All; sgmlReader.CaseFolding = CaseFolding.ToLower; sgmlReader.InputStream = new System.IO.StringReader(htmlString); // create document XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.XmlResolver = null; doc.Load(sgmlReader); XmlElement htmlElement = doc.CreateElement("html"); htmlElement.AppendChild(doc.DocumentElement); // Create well-formed Xml from Html string //XmlElement htmlElement = doc.DocumentElement; //HtmlParser.ParseHtml(htmlString); // Decide what name to use as a root string rootElementName = asFlowDocument ? HtmlToXamlConverter.Xaml_FlowDocument : HtmlToXamlConverter.Xaml_Section; // Create an XmlDocument for generated xaml XmlDocument xamlTree = new XmlDocument(); XmlElement xamlFlowDocumentElement = xamlTree.CreateElement(null, rootElementName, _xamlNamespace); // Extract style definitions from all STYLE elements in the document CssStylesheet stylesheet = new CssStylesheet(htmlElement); // Source context is a stack of all elements - ancestors of a parentElement List<XmlElement> sourceContext = new List<XmlElement>(10); // Clear fragment parent InlineFragmentParentElement = null; // convert root html element AddBlock(xamlFlowDocumentElement, htmlElement, new Hashtable(), stylesheet, sourceContext); // In case if the selected fragment is inline, extract it into a separate Span wrapper if (!asFlowDocument) { xamlFlowDocumentElement = ExtractInlineFragment(xamlFlowDocumentElement); } // Return a string representing resulting Xaml xamlFlowDocumentElement.SetAttribute("xml:space", "preserve"); string xaml = xamlFlowDocumentElement.OuterXml; return xaml; }
public override IEnumerable<System.Xml.XmlReader> GetReaders() { foreach (var textReader in _source) { var sgmlReader = new SgmlReader(); sgmlReader.DocType = "HTML"; sgmlReader.WhitespaceHandling = WhitespaceHandling.All; sgmlReader.CaseFolding = CaseFolding.ToLower; sgmlReader.InputStream = textReader; sgmlReader.SimulatedNode = this.ConformanceLevel == System.Xml.ConformanceLevel.Fragment ? simulatedNode : null; sgmlReader.StripDocType = false; yield return sgmlReader; } }