Beispiel #1
0
        private void OperateXML_Load(object sender, EventArgs e)
        {
            path = @"TestData\XmlCommendService.xml";
            XmlCommendService xmlCommendService = XMLManager.ReadXmlToObject(path);
            List <string>     businessType      = new List <string>();

            foreach (BusinessType business in xmlCommendService.BusinessTypes)
            {
                businessType.Add(business.Name);
            }
            this.cboBusinessTypeName.DataSource    = businessType;
            this.cboBusinessTypeName.SelectedIndex = -1;
        }
Beispiel #2
0
        /// <summary>
        /// 从XML文档中读数据,并转换成XmlCommend
        /// </summary>
        /// <param name="path">xml文档存放的路径</param>
        /// <returns></returns>
        public static XmlCommendService ReadXmlToObject(string path)
        {
            XmlCommendService xmlCommendService = null;

            XmlReader xmlReader = null;

            try
            {
                using (StreamReader streamReader = new StreamReader(path))
                {
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(streamReader.ReadToEnd());
                    streamReader.Close();

                    xmlReader = XmlReader.Create(new MemoryStream(buffer), null);

                    //数据有效性验证
                    while (xmlReader.Read())
                    {
                    }

                    //序列化之后再反序列化
                    XmlSerializer serializer = new XmlSerializer(typeof(XmlCommendService));
                    commandService = xmlCommendService = (XmlCommendService)serializer.Deserialize(new MemoryStream(buffer));

                    return(xmlCommendService);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                }
            }
        }