从xml文件映射到类属性 [英] map from a xml file to class properties

查看:95
本文介绍了从xml文件映射到类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi I have this xml file, and I want to deserialize this xml file to a class which has exactly the same properties like this xml file, I know serialization and deserialization, but I don't know how to map the properties exactly!!! 










<?xml version="1.0" encoding="utf-8" ?> 
- <XML_FILE_FOR_SOME_INPUTS>
- <Printer1>
  <Title>behnoud</Title> 
  <PhoneNumber>22174960</PhoneNumber> 
  <Site>www.sbu.com</Site> 
  <PrinterPort>abc</PrinterPort> 
  </Printer1>
- <Printer2>
  <BillAcceptorPort>abc</BillAcceptorPort> 
  </Printer2>
  </XML_FILE_FOR_SOME_INPUTS>

推荐答案

public class InputToolsClass
    {
        public InputToolsClass()
        {

        }

        public InputToolsClass(string companytitle, string phonenumber, string site, string printer1, string Printer2 )
        {
            this.Title = title;
            this.PhoneNumber = phonenumber;
            this.Site = site;
            this.Printer1 = printer1;
            this.Printer2 = Printer2 ;
        }

        public string Title { get; set; }
        public string PhoneNumber { get; set; }
        public string Site { get; set; }
        public string Printer1 { get; set; }
        public string Printer2 { get; set; }
}


public void SetAllProperties()
        {
            XmlSerializer ser = new XmlSerializer(typeof(InputToolsClass));

            InputToolsClass inputs;

            using (StreamReader reader = new StreamReader(@"C:\Desktop\Inputs.xml"))
            {

                inputs = (InputToolsClass)ser.Deserialize(reader);
            }
        }


XmlRootAttribute root = new XmlRootAttribute("XML_FILE_FOR_SOME_INPUTS");





因此必须添加XmlRootAttribute。



alose it is essential to add XmlRootAttribute.


这篇关于从xml文件映射到类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆