// 获取xml元素中的RTF private string ProcessElement(XElement element, int level) { // viewer不支持有命名空间的XML文件 if (!string.IsNullOrEmpty(element.Name.Namespace.NamespaceName)) { throw new ApplicationException( "This viewer does not support the Xml file that has Namespace."); } string elementRtfFormat = string.Empty; StringBuilder childElementsRtfContent = new StringBuilder(); StringBuilder attributesRtfContent = new StringBuilder(); // 构造indent字符串 string indent = new string(' ', 4 * level); // 如果元素有子元素和值, 添加这个元素到RTF中 // {{0}} 将要被替换为这些属性, {{1}} 将要被替换为子元素或值 if (element.HasElements || !string.IsNullOrWhiteSpace(element.Value)) { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} >\par {{1}} {0}\cf{1} </\cf{2} {3}\cf{1} >\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); // 构造RTF子元素 if (element.HasElements) { foreach (var childElement in element.Elements()) { string childElementRtfContent = ProcessElement(childElement, level + 1); childElementsRtfContent.Append(childElementRtfContent); } } // 如果 !string.IsNullOrWhiteSpace(element.Value), 然后构造RTF的值 else { childElementsRtfContent.AppendFormat(@"{0}\cf{1} {2}\par", new string(' ', 4 * (level + 1)), XMLViewerSettings.ValueID, CharacterEncoder.Encode(element.Value.Trim())); } } // 元素只有这些属性. {{0}} 将要被这些属性替换. else { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} />\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); } // 构造RTF的属性 if (element.HasAttributes) { foreach (XAttribute attribute in element.Attributes()) { string attributeRtfContent = string.Format( @" \cf{0} {3}\cf{1} =\cf0 ""\cf{2} {4}\cf0 """, XMLViewerSettings.AttributeKeyID, XMLViewerSettings.TagID, XMLViewerSettings.AttributeValueID, attribute.Name, CharacterEncoder.Encode(attribute.Value)); attributesRtfContent.Append(attributeRtfContent); } attributesRtfContent.Append(" "); } return(string.Format(elementRtfFormat, attributesRtfContent, childElementsRtfContent)); }
// Get the Rtf of the xml element. private string ProcessElement(XmlNode element, int level) { string elementRtfFormat = string.Empty; StringBuilder childElementsRtfContent = new StringBuilder(); StringBuilder attributesRtfContent = new StringBuilder(); // Construct the indent. string indent = new string(' ', 2 * level); // If the element has child elements or value, then add the element to the // Rtf. {{0}} will be replaced with the attributes and {{1}} will be replaced // with the child elements or value. if (element.ChildNodes.Count > 0 && !(element.ChildNodes.Count == 1 && element.ChildNodes[0] is XmlText)) { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} >\par {{1}} {0}\cf{1} </\cf{2} {3}\cf{1} >\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); // Construct the Rtf of child elements. foreach (XmlNode childElement in element.ChildNodes) { string childElementRtfContent = ProcessElement(childElement, level + 1); childElementsRtfContent.Append(childElementRtfContent); } } else if (element is XmlComment) { elementRtfFormat = string.Format(@" {0}\cf{1} <!-- {{1}} \cf{1} -->\par", indent, XMLViewerSettings.TagID); childElementsRtfContent.AppendFormat(@"{0}\cf{1} {2}", new string(' ', 2 * (0 /*level + 1*/)), XMLViewerSettings.CommentID, element.Value.Replace("\r\n", "\n").Replace("\n", "\\line ")); } // If !string.IsNullOrWhiteSpace(element.Value), then construct the Rtf // of the value. else if (element.ChildNodes.Count == 1 && element.ChildNodes[0] is XmlText) { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} > {{1}} \cf{1} </\cf{2} {3}\cf{1} >\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); childElementsRtfContent.AppendFormat(@"{0}\cf{1} {2}", new string(' ', 2 * (0 /*level + 1*/)), XMLViewerSettings.ValueID, CharacterEncoder.Encode(((XmlText)element.ChildNodes[0]).Value.Trim())); } // If !string.IsNullOrWhiteSpace(element.Value), then construct the Rtf // of the value. else if (!string.IsNullOrWhiteSpace(element.Value)) { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} > {{1}} \cf{1} </\cf{2} {3}\cf{1} >\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); childElementsRtfContent.AppendFormat(@"{0}\cf{1} {2}", new string(' ', 2 * (0 /*level + 1*/)), XMLViewerSettings.ValueID, CharacterEncoder.Encode(element.Value.Trim())); } // This element only has attributes. {{0}} will be replaced with the attributes. else { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} />\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); } // Construct the Rtf of the attributes. if (element.Attributes != null && element.Attributes.Count > 0) { foreach (XmlAttribute attribute in element.Attributes) { string attributeRtfContent = string.Format( @" \cf{0} {3}\cf{1} =\cf0 {5}\cf{2} {4}\cf0 {5}", XMLViewerSettings.AttributeKeyID, XMLViewerSettings.TagID, XMLViewerSettings.AttributeValueID, attribute.Name, CharacterEncoder.Encode(attribute.Value), settings.QuoteCharacter); attributesRtfContent.Append(attributeRtfContent); } attributesRtfContent.Append(" "); } return(string.Format(elementRtfFormat, attributesRtfContent, childElementsRtfContent)); }
// Get the Rtf of the xml element. private string ProcessElement(XElement element, int level) { // This viewer does not support the Xml file that has Namespace. if (!string.IsNullOrEmpty(element.Name.Namespace.NamespaceName)) { throw new ApplicationException( "This viewer does not support the Xml file that has Namespace."); } string elementRtfFormat = string.Empty; StringBuilder childElementsRtfContent = new StringBuilder(); StringBuilder attributesRtfContent = new StringBuilder(); // Construct the indent. string indent = new string(' ', 4 * level); // If the element has child elements or value, then add the element to the // Rtf. {{0}} will be replaced with the attributes and {{1}} will be replaced // with the child elements or value. if (element.HasElements || !string.IsNullOrWhiteSpace(element.Value)) { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} >\par {{1}} {0}\cf{1} </\cf{2} {3}\cf{1} >\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); // Construct the Rtf of child elements. if (element.HasElements) { foreach (var childElement in element.Elements()) { string childElementRtfContent = ProcessElement(childElement, level + 1); childElementsRtfContent.Append(childElementRtfContent); } } // If !string.IsNullOrWhiteSpace(element.Value), then construct the Rtf // of the value. else { childElementsRtfContent.AppendFormat(@"{0}\cf{1} {2}\par", new string(' ', 4 * (level + 1)), XMLViewerSettings.ValueID, CharacterEncoder.Encode(element.Value.Trim())); } } // This element only has attributes. {{0}} will be replaced with the attributes. else { elementRtfFormat = string.Format(@" {0}\cf{1} <\cf{2} {3}{{0}}\cf{1} />\par", indent, XMLViewerSettings.TagID, XMLViewerSettings.ElementID, element.Name); } // Construct the Rtf of the attributes. if (element.HasAttributes) { foreach (XAttribute attribute in element.Attributes()) { string attributeRtfContent = string.Format( @" \cf{0} {3}\cf{1} =\cf0 ""\cf{2} {4}\cf0 """, XMLViewerSettings.AttributeKeyID, XMLViewerSettings.TagID, XMLViewerSettings.AttributeValueID, attribute.Name, CharacterEncoder.Encode(attribute.Value)); attributesRtfContent.Append(attributeRtfContent); } attributesRtfContent.Append(" "); } return(string.Format(elementRtfFormat, attributesRtfContent, childElementsRtfContent)); }