xml into an object

Three steps

i. convert dictionary
ii. Set conversion dictionary to JSON
iii.json transducer into an object

/// <summary>
/// 转换为字典信息
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public SortedDictionary<string, object> FromXml(string xml)
{
    SortedDictionary<string, object> m_values = new SortedDictionary<string, object>();
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
    XmlNodeList nodes = xmlNode.ChildNodes;
    foreach (XmlNode xn in nodes)
    {
        XmlElement xe = (XmlElement)xn;
        m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
    }
        return m_values;
    }

    SortedDictionary<string, object> dic= FromXml(req);
    string temp = JsonConvert.SerializeObject(dic);
    xxx obj= JsonConvert.DeserializeObject<xxx>(temp);//反序列化对象
}

Guess you like

Origin www.cnblogs.com/pengyinghao/p/11289920.html