WPF动态生成XML流文档(带缩进和颜色,以及属性排序和换行缩进)

6 篇文章 0 订阅
3 篇文章 0 订阅

功能概述:

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);

        }

	}
}

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值