private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { var tagNameToUpper = (child as XmlElement)?.TagName?.ToUpper(); if (!isInP && !(child is XmlElement && (tagNameToUpper == "P" || tagNameToUpper == "SPAN" || tagNameToUpper == "DIV" || tagNameToUpper == "IMG"))) { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; parent = new ParagraphTextContainer(parent, paragraph); } if (child is XmlText) { var text = child.InnerText.Replace("\n", "").Trim(); if (string.IsNullOrEmpty(text)) { continue; } text = text.Replace("</form>", ""); parent.Add(new Run { Text = System.Net.WebUtility.HtmlDecode(text), Foreground = new SolidColorBrush(Windows.UI.Colors.Black) }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (tagNameToUpper) { case "P": case "DIV": case "SPAN": if (tagNameToUpper == "DIV") { var classVal = e.Attributes.FirstOrDefault(a => a.NodeName == "class")?.NodeValue?.ToString() ?? ""; if (classVal.Contains("open-list-comments") || classVal.Contains("open-list-footer") || classVal.Contains("modal-body")) { return; } } if (isInP) { if (tagNameToUpper == "P") { parent.Add(new LineBreak()); } ParseElement(e, parent); } else { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; ParseElement(e, new ParagraphTextContainer(parent, paragraph)); isInP = false; } break; case "B": case "STRONG": case "H1": case "H2": case "H3": case "H4": case "H5": case "H6": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(parent, bold)); break; case "I": case "EM": var italic = new Italic(); parent.Add(italic); ParseElement(e, new SpanTextContainer(parent, italic)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(parent, underline)); break; case "A": var url = e.Attributes.FirstOrDefault(a => a.NodeName == "href")?.NodeValue; Uri uri; if (url != null && Uri.TryCreate(url.ToString(), UriKind.Absolute, out uri)) { var link = new Hyperlink(); link.NavigateUri = uri; parent.Add(link); ParseElement(e, new SpanTextContainer(parent, link)); } else { ParseElement(e, parent); } break; case "BR": parent.Add(new LineBreak()); break; case "IMG": var src = e.Attributes.FirstOrDefault(a => a.NodeName == "src")?.NodeValue; Uri srcUri; if (src != null && Uri.TryCreate(src.ToString(), UriKind.Absolute, out srcUri)) { var img = new InlineUIContainer { Child = new Windows.UI.Xaml.Controls.Image() { Source = new BitmapImage(srcUri) } }; parent.AddToParentParagraph(img); } break; } } } }
private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { var tagNameToUpper = (child as XmlElement)?.TagName?.ToUpper(); if (!isInP && !(child is XmlElement && (tagNameToUpper == "P" || tagNameToUpper == "SPAN" || tagNameToUpper == "DIV" || tagNameToUpper == "IMG"))) { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; parent = new ParagraphTextContainer(parent, paragraph); } if (child is XmlText) { var text = child.InnerText.Replace("\n", "").Trim(); if (string.IsNullOrEmpty(text)) continue; text = text.Replace("</form>", ""); parent.Add(new Run { Text = System.Net.WebUtility.HtmlDecode(text), Foreground = new SolidColorBrush(Windows.UI.Colors.Black) }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (tagNameToUpper) { case "P": case "DIV": case "SPAN": if(tagNameToUpper == "DIV") { var classVal = e.Attributes.FirstOrDefault(a => a.NodeName == "class")?.NodeValue?.ToString() ?? ""; if(classVal.Contains("open-list-comments") || classVal.Contains("open-list-footer") || classVal.Contains("modal-body")) { return; } } if (isInP) { if (tagNameToUpper == "P") parent.Add(new LineBreak()); ParseElement(e, parent); } else { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; ParseElement(e, new ParagraphTextContainer(parent, paragraph)); isInP = false; } break; case "B": case "STRONG": case "H1": case "H2": case "H3": case "H4": case "H5": case "H6": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(parent, bold)); break; case "I": case "EM": var italic = new Italic(); parent.Add(italic); ParseElement(e, new SpanTextContainer(parent, italic)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(parent, underline)); break; case "A": var url = e.Attributes.FirstOrDefault(a => a.NodeName == "href")?.NodeValue; Uri uri; if (url != null && Uri.TryCreate(url.ToString(), UriKind.Absolute, out uri)) { var link = new Hyperlink(); link.NavigateUri = uri; parent.Add(link); ParseElement(e, new SpanTextContainer(parent, link)); } else { ParseElement(e, parent); } break; case "BR": parent.Add(new LineBreak()); break; case "IMG": var src = e.Attributes.FirstOrDefault(a => a.NodeName == "src")?.NodeValue; Uri srcUri; if (src != null && Uri.TryCreate(src.ToString(), UriKind.Absolute, out srcUri)) { var img = new InlineUIContainer { Child = new Windows.UI.Xaml.Controls.Image() { Source = new BitmapImage(srcUri) } }; parent.AddToParentParagraph(img); } break; } } } }