功能概述:
1.根据XML文本生成动态链表结构,具体的生成机制已经在相关博客论坛做过描述,这里不再赘述,不然就跑题了
解析XML的完整代码已上传至GitHub
https://github.com/PengChen01/CodeAnalyseProject/blob/master/AnalyseXML.cs
2.根据XML文本生成的动态链表结构,通过递归的方式生成树状的深度链表结构,具体的生成机制已经在相关博客论坛做过描述,这里不再赘述,不然就跑题了
解析XML的完整代码已上传至GitHub
https://github.com/PengChen01/CodeAnalyseProject/blob/master/AnalyseXML.cs
3.根据深度链表构建WPF流文档
internal void appendXML(string xml)
{
appendNestXMLtoParagraph(LoginSAP.AnalyseXML.getNestXML(xml)[0].subtag,"");
}
private void appendNestXMLtoParagraph(List<LoginSAP.AnalyseXML.tagNest> tagNest,string blank)
{
Paragraph paragraph;
Span span;
blank += " ";
int attrPadding = 0;
for (int i = 0; i < tagNest.Count; i++)
{
paragraph = new Paragraph();
paragraph.FontSize = 12.67;
paragraph.FontFamily = new FontFamily("NSimSun");
paragraph.Margin = new Thickness(0);
span = new Span(new Run(blank + "<"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
span = new Span(new Run(tagNest[i].tagName));
span.Foreground = Brushes.Brown;
paragraph.Inlines.Add(span);
if (tagNest[i].Attribute.Count>3)
{
IOrderedEnumerable<KeyValuePair<string, object>> query = tagNest[i].Attribute.OrderBy(s => s.Key);//属性多于3个,先排序,优化搜索
span = new Span(new Run(" " + query.ElementAt(0).Key));
span.Foreground = Brushes.Red;
paragraph.Inlines.Add(span);
span = new Span(new Run("=" + query.ElementAt(0).Value));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
this.richTextBox1.Document.Blocks.Add(paragraph);
attrPadding = blank.Length + 2 + tagNest[i].tagName.Length;
for (int j = 1; j < query.Count(); j++)
{
//paragraph = new Paragraph();
//paragraph.FontSize = 12.67;
//paragraph.FontFamily = new FontFamily("NSimSun");
//paragraph.Margin = new Thickness(0);
paragraph.Inlines.Add(new LineBreak());
span = new Span(new Run(" ".PadLeft(attrPadding,' ') + query.ElementAt(j).Key));
span.Foreground = Brushes.Red;
paragraph.Inlines.Add(span);
span = new Span(new Run("=" + query.ElementAt(j).Value));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
}
}
else
{
for (int j = 0; j < tagNest[i].Attribute.Count; j++)
{
span = new Span(new Run(" " + tagNest[i].Attribute.ElementAt(j).Key));
span.Foreground = Brushes.Red;
paragraph.Inlines.Add(span);
span = new Span(new Run("=" + tagNest[i].Attribute.ElementAt(j).Value));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
}
}
if (tagNest[i].subtag.Count ==0)
{
if (String.IsNullOrEmpty(tagNest[i].content))
{
span = new Span(new Run("/>"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
this.richTextBox1.Document.Blocks.Add(paragraph);
}
else
{
span = new Span(new Run(">"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
span = new Span(new Run(tagNest[i].content));
span.Foreground = Brushes.DarkGreen;
span.FontWeight = FontWeights.Bold;
paragraph.Inlines.Add(span);
span = new Span(new Run("</"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
span = new Span(new Run(tagNest[i].tagName));
span.Foreground = Brushes.Brown;
paragraph.Inlines.Add(span);
span = new Span(new Run(">"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
this.richTextBox1.Document.Blocks.Add(paragraph);
}
}
else
{
span = new Span(new Run(">"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
this.richTextBox1.Document.Blocks.Add(paragraph);
appendNestXMLtoParagraph(tagNest[i].subtag, blank);
paragraph = new Paragraph();
paragraph.FontSize = 12.67;
paragraph.FontFamily = new FontFamily("NSimSun");
paragraph.Margin = new Thickness(0);
span = new Span(new Run(blank + "</"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
span = new Span(new Run(tagNest[i].tagName));
span.Foreground = Brushes.Brown;
paragraph.Inlines.Add(span);
span = new Span(new Run(">"));
span.Foreground = Brushes.Blue;
paragraph.Inlines.Add(span);
this.richTextBox1.Document.Blocks.Add(paragraph);
}
}
}
效果