C#将XML文档解析为实体对象

  public static T XmlDataToModel<T>(String xmlData)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlData);

        XmlElement element = xmlDoc.DocumentElement;

        T objModel = System.Activator.CreateInstance<T>();
        foreach (XmlNode childNode in element.ChildNodes)
        {
            PropertyInfo pi = objModel.GetType().GetProperty(childNode.Name);
            if (pi == null) continue;
            if (!String.IsNullOrEmpty(childNode.InnerXml.Trim()))
                pi.SetValue(objModel, childNode.InnerXml, null);
        }

        return objModel;
    }

  调用:XMLHelper.XmlDataToModel<实体对象类>(封装实体信息的string类型XML);