C# xml 转 实体类 同时获取 属性值 节点值

有用留个言,谢谢... 

        private void btnCustomXml_Click(object sender, EventArgs e)
        {
            var str = "aaa";
            var obj = StringToXml(str);

            int dsad = 0;
        }

        public T StringToXml(string strXML) 
        {
            try
            {
                using (StringReader sr = new StringReader(strXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return (T)serializer.Deserialize(sr);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("将XML转换成实体对象异常", ex);
            }
        }





    [XmlRootAttribute("root")]
    public class EntityXmlCustom
    {
        [XmlElementAttribute("node")]
        public EntityXmlCustomNode Node { get; set; }
        
    }
    [XmlRootAttribute("node")]
    public class EntityXmlCustomNode
    {
        [XmlAttribute("attr")]
        public string Attr { get; set; }

        [System.Xml.Serialization.XmlText]
        public string Value { get; set; }
    }

有用留个言,谢谢...

 

 

你可能感兴趣的:(C#)