博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取iOS plist文件 (其实类似读取xml文件)
阅读量:4453 次
发布时间:2019-06-07

本文共 9392 字,大约阅读时间需要 31 分钟。

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; }
}
}

转载于:https://www.cnblogs.com/Nine4Cool/p/10540674.html

你可能感兴趣的文章
vue-textarea 自适应高度
查看>>
(2)数据结构——线性表(链表)实现
查看>>
[leetCode]Linked List Cycle I+II
查看>>
leetcode中的python学习
查看>>
sqlserver打开对象资源管理器管理的帮助文档的快捷键
查看>>
JBOSSAS 5.x/6.x 反序列化命令执行漏洞(CVE-2017-12149)
查看>>
Zookeeper zkui-zookeeper图形化管理工具
查看>>
java运行时内存分类
查看>>
为什么说 Git 比 SVN 更好
查看>>
1.基础数据类型的初识 字符串 bool 整型 if else elif
查看>>
【设计模式】4、原型模式
查看>>
进入meta模式关闭背光灯
查看>>
webstorm上svn的安装使用
查看>>
【JEECG技术文档】数据权限自定义SQL表达式用法说明
查看>>
使用 Bootstrap Typeahead 组件
查看>>
EF不能很好的支持DDD?估计是我们搞错了!
查看>>
ubuntu下基于sqlite3后台的php环境的搭建
查看>>
Qt 静态库与共享库(动态库)共享配置的一个小办法
查看>>
linux_cacti 配置之 安装snmp 服务
查看>>
201407-至今
查看>>