/// <summary>
/// xml字符串转换成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xmlStr"></param>
/// <returns></returns>
public T XMLStringToObject<T>(string xmlStr)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr); //加载xml文件
//去掉头
doc.RemoveChild(doc.FirstChild);
string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc).Replace("@", "");
//选择开始解析的节点
if (json.StartsWith("{\"response\""))
{
JObject obj = JObject.Parse(json);
json = obj.GetValue("response").ToString();
}
//转化为对象
Type t = typeof(T);
var entiy = Activator.CreateInstance(t);
entiy = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
return (T)entiy;
}
xml字符串转换成对象
最新推荐文章于 2024-04-12 22:04:42 发布