wpf使用c#实现xml文件的反序列化(通过List)

记录今天的工作日常

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("文件正确");
                        }
                    }
                }
          }   

       }
    }
}

  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值