记录今天的工作日常
1.xml文件如下图所示
第一步,在xaml中创建一个按钮
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Height="44" Margin="241,204,0,0" VerticalAlignment="Top" Width="87" Click="Button_Click"/>
</Grid>
第二步,根据xml文件创建相应的类(这里是单独创建,也可一起创建到主程序中)
public class root
{
public FileInfo FileInfo { get; set; }
public Slice Slice { get; set; }
public Machines Machines { get; set; }
public Layers Layers { get; set; }
}
public class FileInfo
{
public Version Version { get; set; }
public Written Written { get; set; }
}
public class Verson
{
public string Major { get; set; }
public string Minor { get; set; }
}
public class Written
{
public string By { get; set; }
public string ApplicationName { get; set; }
public string ApplicationVersion { get; set; }
public string When { get; set; }
}
public class Slice
{
public int Count { get; set; }
public double thickness { get; set; }
public double StartHeight { get; set; }
public double endHeight { get; set; }
public int layersCount { get; set; }
}
public class Machines
{
public string Resolutio { get; set; }
public string Pixe1XSize { get; set; }
public string Pixe1YSize { get; set; }
public string xmlString = "<Anti-Aliasing>1</Anti-Aliasing>";
public int XLength { get; set; }
public int YWidth { get; set; }
public int ZHeigth { get; set; }
}
public class Layers
{
[XmlElement("Layer")]
public List<Layer> LayerList { get; set; }
}
}
public class Layer
{
public int index { get; set; }
public double zvalue { get; set; }
public string filename { get; set; }
}
第三步,写主程序
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string filePath;
private Stream fs;
public IEnumerable<Layer> Layerlist { get; private set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "文本文件 (*.xml)|*.xml|所有文件 (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
filePath = openFileDialog.FileName;
MessageBox.Show($"成功导入文件:{filePath}");
xmlDeseriallize($"{filePath}");
}
}
private void xmlDeseriallize(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(root));
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
root deserializedObject = (root)serializer.Deserialize(fileStream);
}
CheckLayers();
}
private void CheckLayers()
{
{
if (Layerlist != null)
{
foreach (Layer per in Layerlist)
{
if (per.index < 200 || per.zvalue < 10)
{
MessageBox.Show("切片文件不完整");
}
else
{
MessageBox.Show("文件正确");
}
}
}
}
}
}
}