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);
}
}
}
}
}
C#数据转化+BFS拷贝
最新推荐文章于 2024-06-15 09:07:54 发布