using System;
using System.Data;namespace Manjinba.Communication.Common.Utils
{ public class PlistRead { ///<summary> ///以文件流模式读取整个XML文档内容并转换为DataSet ///</summary> public static DataSet Getds(string XmlFile) { DataSet ds = new DataSet(); try { string RoadStr = System.AppDomain.CurrentDomain.BaseDirectory + XmlFile; System.IO.FileStream fs = new System.IO.FileStream(RoadStr, System.IO.FileMode.Open, System.IO.FileAccess.Read); ds.ReadXml(fs);fs.Close();
} catch (Exception ex) { string error = ex.Message; } return ds; } }}
using Manjinba.Communication.Common.Logging;
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Xml.Linq;using System.Xml.Serialization;namespace Manjinba.Communication.Common.Utils
{ public class ReadPlist { public List<DataType> DateList { get; set; } //int saveid = 1; DataType RootNode; int count; //int itemCount; public ReadPlist() { count = 1; //itemCount = 0; RootNode = new DataType(); RootNode.ID = count; RootNode.DataName = null; RootNode.parentID = 0; RootNode.ValueType = EnumValueType.DICT; RootNode.childrenID = new List<int>(); this.DateList = new List<DataType>(); DateList.Add(RootNode);}
public XDocument LoadFromFile(string path) { return XDocument.Load(path); } public void XMLparser(string path) { XDocument doc = LoadFromFile(path); XElement FirstElement = doc.Root.Element("dict"); DateList[0].childrenID = XMLOnce(FirstElement, 1); foreach (var item in DateList) { if (item.Value == "FALSE" || item.Value == "TRUE") { item.Value = item.Value.ToLower(); } try { if (Char.IsNumber(item.DataName[0])) { item.DataName.Insert(0, "_"); } } catch { }}
//print(); } public List<int> XMLOnce(XElement nowElement, int parentid) { List<DataType> DataTemp = new List<DataType>(); List<int> IDList = new List<int>(); List<int> childrenIDList = new List<int>(); var keys = from k in nowElement.Elements("key") select k; var values = from v in nowElement.Elements() where v.Name != "key" select v; for (int i = 0; i < values.ToList().Count; i++) { int id = ++count; EnumValueType valuetype = (EnumValueType)Enum.Parse(typeof(EnumValueType), values.ToList()[i].Name.LocalName.ToString().ToUpper(), true); string value = null; if (valuetype == EnumValueType.ARRAY) { XElement newElement = nowElement.Elements().Except(nowElement.Elements("key")).ElementAt(i); int num = newElement.Elements().Count(); for (int j = 0; j < num; j++) { newElement.AddFirst(new XElement("key", "item")); } childrenIDList = XMLOnce(newElement, id); } else if (valuetype == EnumValueType.DICT) { XElement newElement = nowElement.Elements().Except(nowElement.Elements("key")).ElementAt(i); childrenIDList = XMLOnce(newElement, id); } else { value = values.ToList()[i].Value.ToString(); }try
{ DataTemp.Add(new DataType() { DataName = keys.ToList()[i].Value.ToString(), ValueType = valuetype, ID = id, Value = value, parentID = parentid, childrenID = childrenIDList }); } catch { DataTemp.Add(new DataType() { DataName = "itemContent", ValueType = valuetype, ID = id, Value = value, parentID = parentid, childrenID = childrenIDList }); }}
foreach (var item in DataTemp) { IDList.Add(item.ID); } DateList.AddRange(DataTemp); return IDList; } }public class ConvertXML
{ List<DataType> DataList { get; set; } public ConvertXML(List<DataType> datalist) { DataList = datalist; } //public XDocument xdoc = new XDocument(new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Model"))); public XDocument xdoc = new XDocument(new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("iOSPlist"))); //public XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", null)); public XDocument creatXML() { //var xel = new XElement("iOSPlist"); //xdoc.Add(xel); //xdoc.Element("Model").SetAttributeValue("id", "1"); xdoc.Element("iOSPlist").SetAttributeValue("id", "1"); foreach (var item in DataList) { if (DataList[0].childrenID.Contains(item.ID)) { //xdoc.Element("Model").Add(new XElement(item.DataName, item.Value)); XElement newElement = xdoc.Descendants().Where(e => e.Attribute("id").Value == "1").First(); //XElement newElement = xdoc.Descendants().First(); newElement.Add(new XElement(item.DataName, item.Value, new XAttribute("id", item.ID))); //newElement.Add(new XElement(item.DataName, item.Value)); if (item.ValueType == EnumValueType.ARRAY || item.ValueType == EnumValueType.DICT) { //XElement newElement = xdoc.Element("Model").Element(item.DataName); creatOnce(newElement, item); }}
} return xdoc; } public void creatOnce(XElement doc, DataType parent) { foreach (var item in DataList) { if (parent.childrenID.Contains(item.ID)) { string parentID = parent.ID.ToString(); //string parentName = parent.DataName; XElement newElement = doc.Descendants().Where(e => e.Attribute("id").Value.Equals(parentID)).First(); //XElement newElement = doc.Descendants().Where(e=> parentName.Equals(e.Name.ToString())).Last(); newElement.Add(new XElement(item.DataName.Replace('-', '_'), item.Value, new XAttribute("id", item.ID))); //newElement.Add(new XElement(item.DataName.Replace('-', '_'), item.Value)); if (item.ValueType == EnumValueType.ARRAY || item.ValueType == EnumValueType.DICT) { //nowElement = nowElement.Element(item.DataName); creatOnce(newElement, item); } } } } } public class PlistSerializer : XmlSerializer { /// <summary> /// /// </summary> /// <param name="path"></param> /// <returns></returns> public static string GetVersionNum(string path) { try { MemoryStream stream = new MemoryStream(); ReadPlist rp = new ReadPlist(); rp.XMLparser(path); ConvertXML cx = new ConvertXML(rp.DateList); XDocument doc = cx.creatXML(); return doc.Descendants().Where(e => "bundle_version".Equals(e.Name.ToString())).Select(s => s.Value).First(); } catch (Exception e) { LogHelper.GetLog(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName).Error(e.Message.ToString() + e.StackTrace); return "-1"; } } public static iOSVersionInfo GetiOSVersionInfo(string path) { MemoryStream stream = new MemoryStream(); ReadPlist rp = new ReadPlist(); rp.XMLparser(path); ConvertXML cx = new ConvertXML(rp.DateList); XDocument doc = cx.creatXML();iOSVersionInfo iOSVersion = new iOSVersionInfo();
iOSVersion.bundleVersion = doc.Descendants().Where(e => "bundle_version".Equals(e.Name.ToString())).Select(s => s.Value).First(); var isForceUpdate = doc.Descendants().Where(e => "forceUpdate".Equals(e.Name.ToString())).Select(s => s.Value).FirstOrDefault(); Int32.TryParse(isForceUpdate, out Int32 isForce); iOSVersion.forceUpdate = isForce; return iOSVersion; } /// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="assemblyName"></param> /// <returns></returns> public static object Deserialize(string path, string assemblyName) { MemoryStream stream = new MemoryStream(); ReadPlist rp = new ReadPlist(); rp.XMLparser(path); ConvertXML cx = new ConvertXML(rp.DateList); //Console.WriteLine(cx.creatXML()); XDocument doc = cx.creatXML(); doc.Save(stream); stream.Seek(0, SeekOrigin.Begin);try
{ StreamReader sr = new StreamReader(stream); stream.Position = 0; string text = sr.ReadToEnd(); sr.Close(); stream.Close();text = text.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
text = text.Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");var mySerializer = new XmlSerializer(typeof(iOSPlist));
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(text))) { using (var streamReader = new StreamReader(ms, Encoding.UTF8)) { return (iOSPlist)mySerializer.Deserialize(streamReader); } } } catch (Exception e) { LogHelper.GetLog(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName).Error(e.Message.ToString() + e.StackTrace); stream.Close(); return null; }}
}public enum EnumValueType
{ DICT, ARRAY, NUMBER, STRING, DTAE, BOOLEAN, DATA, INTEGER }public class DataType
{ public int ID { get; set; } public string DataName { get; set; } public EnumValueType ValueType { get; set; } public string Value { get; set; } public int parentID { get; set; } public List<int> childrenID { get; set; } }public class iOSVersionInfo
{ public string bundleVersion { get; set; } public int forceUpdate { get; set; } }}
using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Manjinba.Communication
{ /// <summary> /// /// </summary> public class iOSPlist { public iOSPlist() { //items.Add("assets", new assets()); } /// <summary> /// /// </summary> public List<assetsmetadata> items { get; set; } }public class assetsmetadata
{ /// <summary> /// /// </summary> public assets assets { get; set; } /// <summary> /// /// </summary> public metadata metadata { get; set; } }/// <summary>
/// /// </summary> public class assets { /// <summary> /// /// </summary> public List<asset> assetsDic { get; set; }}
public class asset { /// <summary> /// /// </summary> public string kind { get; set; } /// <summary> /// /// </summary> public string url { get; set; } }public class metadata
{ /// <summary> /// /// </summary> public string bundle_identifier { get; set; } /// <summary> /// /// </summary> public string bundle_version { get; set; } /// <summary> /// /// </summary> public string kind { get; set; } /// <summary> /// /// </summary> public string title { get; set; } }}