我正在创建一个Windows服务,通过为正在安装的软件创建MD5哈希并将其与已验证软件的MD5哈希进行比较来检查系统上安装的软件的完整性。经过验证的哈希包含在XML文件中,如下所示:
WM_LBUTTONUP
如何使用c#中的MD5GoldenHash标记读取所有数据,以便我可以将正在安装的软件的MD5哈希值与已经验证的哈希值进行比较?任何帮助或提示将不胜感激。
答案 0 :(得分:0)
XmlSerializer
反序列化XML文件的内容。以下是第1步将为您生成的类:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class AppList
{
private AppListVLC[] vLCField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("VLC")]
public AppListVLC[] VLC
{
get
{
return this.vLCField;
}
set
{
this.vLCField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class AppListVLC
{
private string pathField;
private string mD5GoldenHashField;
/// <remarks/>
public string Path
{
get
{
return this.pathField;
}
set
{
this.pathField = value;
}
}
/// <remarks/>
public string MD5GoldenHash
{
get
{
return this.mD5GoldenHashField;
}
set
{
this.mD5GoldenHashField = value;
}
}
}
以下是序列化和反序列化的方法:
public static void Main()
{
var serializer = new XmlSerializer(typeof(AppList));
var reader = new StreamReader("YourFile.xml");
var result = serializer.Deserialize(reader) as AppList;
reader.Close();
foreach (var thisVlc in result.VLC)
{
Console.WriteLine(thisVlc.MD5GoldenHash);
}
// if you want to make changes to xml file then do the following
result.VLC[0].MD5GoldenHash = "Something to show modificaition";
serializer.Serialize(new StreamWriter("YourFileOrSomeOtherFile.xml"), result);
}
答案 1 :(得分:0)
我发现我可以读取特定标记的数据并将其放在这样的列表中:
private List<string> getGoldenHashes(string xml)
{
List<string> list = new List<string>();
int i = 0;
XDocument doc = XDocument.Load(xml);
var goldHashes = doc.Descendants("MD5GoldenHash");
foreach (var gh in goldHashes)
{
list.Add(gh.Value.ToString());
}
return list;
}