C#数据转化+BFS拷贝

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;
using System.Xml.Serialization;

namespace trans
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        #region 字段
        string path = "";
        AnalysisTools ans = new AnalysisTools();
        #endregion
        #region 构造函数
        public MainWindow()
        {

            InitializeComponent();


        }
        #endregion
        #region 方法
        private void Init()
        {
            try
            {
                AnalysisData();
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 事件
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            Init();

        }
        #endregion

        public void Showtree()
        {

        }
 /*       public void Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            string line;
            byte[] content;
            Console.WriteLine(1);
            while ((line = sr.ReadLine()) != null)
            {
                content = Encoding.UTF8.GetBytes(line);
                FFData root1 = ans.FFH1_PDU(content, 2);
                //data1.ItemsSource=
            }
       }
 */
        public void AnalysisData()
        {
            AnalysisTools a = new AnalysisTools();
            List<FFData> itemList = new List<FFData>();
            UploadData up = new UploadData();
            up.Protocols = new List<ProtocolMessage>();
            string path = "";
            using (FileStream stream = new FileStream(@"FF_Monitor.txt", FileMode.OpenOrCreate))
            using (StreamReader streamReader = new StreamReader(stream))
            {

                while (false == streamReader.EndOfStream)
                {
                    byte[] data = ConvertFromString(streamReader.ReadLine());
                    FFData fData = a.FFH1_PDU(data, data.Length - 2);
                    ///
                    List<AnalysisNode> analist = new List<AnalysisNode>();
                    ProtocolMessage prom = new ProtocolMessage();
                    prom.SourceData = data;
                    ///
                    if (null != fData)
                    {
                        itemList.Add(fData);
                        ///
                        AnalysisNode newroot = new AnalysisNode();
                        breadthFirst(fData, newroot);
                        analist.Add(newroot);
                        tra(newroot);
                        ///
                    }
                    prom.Nodes = analist;
                    up.Protocols.Add(prom);
                }

                datalist.ItemsSource = itemList;
            }
            SeriaLizetodata(path, up);
        }
        private byte[] ConvertFromString(string strValue)
        {
            try
            {
                if (true == string.IsNullOrEmpty(strValue.Trim()))
                {
                    return new byte[] { };

                }
                string[] strArray = strValue.Trim().Split(' ');

                List<byte> list = new List<byte>();

                for (int i = 0; i < strArray.Length; i++)
                {
                    list.Add(Convert.ToByte(strArray[i], 16));
                }

                return list.ToArray();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void datalist_selectionchange(object sender, SelectionChangedEventArgs e)
        {
            FFData tree = datalist.SelectedItem as FFData;
            List<FFData> item = new List<FFData>();
            item.Add(tree);
            datatree.ItemsSource = item;
            AnalysisNode newroot = new AnalysisNode();
            breadthFirst(tree,newroot);
            tra(newroot);
        }


        /*
         * 广度优先
         */
        public void breadthFirst(FFData root,AnalysisNode newroot)
        {
            Queue<FFData> nodeDeque = new Queue<FFData>();
            Queue<AnalysisNode> newQueue = new Queue<AnalysisNode>();
            AnalysisNode parent = newroot;
            FFData node = root;
            ///Trace.WriteLine(root.Childs);
            nodeDeque.Enqueue(node);
            newQueue.Enqueue(parent);
            parent.Childs = new List<AnalysisNode>();
            while (nodeDeque.Count()!=0)
            {
                node = nodeDeque.Dequeue();
                parent = newQueue.Dequeue();
                copy(node, parent);
                ///Trace.WriteLine(node.Content);
                if(null != node.Childs)
                foreach (FFData treeNode in node.Childs)
                {
                    nodeDeque.Enqueue(treeNode);
                    if (node.Childs.Count!=0)
                    {
                    AnalysisNode temp1 = new AnalysisNode();
                    copy(treeNode, temp1);
                    parent.Childs.Add(temp1);
                    newQueue.Enqueue(temp1);
                    temp1.Childs = new List<AnalysisNode>();
                    }
                }
            }
        }

        private static void SeriaLizetodata(string path, UploadData up)
        {
            try
            {
                //创建需要序列化的对象

                //创建流 path表示存储xml的文件路径

                FileStream fStream = File.OpenWrite(path);
                using (TextWriter tWriter = new StreamWriter(fStream))
                {
                    XmlSerializer xmlS = new XmlSerializer(typeof(UploadData));
                    xmlS.Serialize(tWriter, up);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void copy(FFData root,AnalysisNode node)
        {
            node.Value = root.Content;
        }

        public void tra(AnalysisNode root)
        {
            Queue<AnalysisNode> nodeDeque = new Queue<AnalysisNode>();
            AnalysisNode node = root;
            nodeDeque.Enqueue(node);
            while (nodeDeque.Count() != 0)
            {
                node = nodeDeque.Dequeue();
                Trace.WriteLine(node.Value);
                foreach (AnalysisNode treeNode in node.Childs)
                {
                    nodeDeque.Enqueue(treeNode);
                }
            }
        }


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值