/// <summary> /// 转换成Moldel /// </summary> /// <param name="xml"></param> /// <returns></returns> public static UserGuideInfo ToModel(string xml) { if (string.IsNullOrEmpty(xml)) { return(null); } XmlDocument xmlDoc = new XmlDocument(); UserGuideInfo model = new UserGuideInfo(); try { xmlDoc.LoadXml(xml); //查找根节点 XmlNodeList nodes = xmlDoc.SelectNodes("//Root/GuideImage/Image"); foreach (XmlNode item in nodes) { model.ImageFilePath.Add(item.Attributes["Url"].Value); } return(model); } catch (Exception ex) { throw ex; } }
/// <summary> /// 转换成Xml /// </summary> /// <param name="model"></param> /// <returns></returns> public static string ToXml(UserGuideInfo model) { //TODO:转换成xml结构的算法 //创建一个xml对象 XmlDocument xmlDoc = new XmlDocument(); //创建开头 XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(dec); //创建根节点 XmlElement root = xmlDoc.CreateElement("Root"); //创建二级节点 XmlElement SecNode = xmlDoc.CreateElement("GuideImage"); foreach (string item in model.ImageFilePath) { XmlElement ThrNode = xmlDoc.CreateElement("Image"); ThrNode.SetAttribute("Url", item); SecNode.AppendChild(ThrNode); } //在根节点中添加二级节点 root.AppendChild(SecNode); //添加根节点 xmlDoc.AppendChild(root); return(xmlDoc.OuterXml); }