1

I am creating a Windows service for checking the integrity of software being installed on a system by creating an MD5 hash for the software being installed and comparing it with the MD5 hashes of verified software. The verified hashes are contained in an XML file that looks like this:

<AppList>
<VLC>
<Path>C:Program Files\VLC\VLC.exe</Path>
<MD5GoldenHash> 1f74882b7a5c8a6ca38912df9786c1cc</MD5GoldenHash>
</VLC>
…
</AppList>

How do I read in all the data with the MD5GoldenHash tag in c# so that I can compare the MD5 hash of the software being installed with the already verified hashes? Any help or tips would be much appreciated.

jp3434
  • 49
  • 7
  • Have you considered just using an XML serializer? Then you could just iterate over a list of objects – BradleyDotNET May 23 '17 at 00:41
  • There are many ways of parsing xml. Serialization is one method which is good when you are parsing the entire xml. If you only need to get one object I recommend xml linq. – jdweng May 23 '17 at 06:12

2 Answers2

0
  1. Use my answer here to create a C# class that represent your XML.
  2. Use the XmlSerializer to deserialize the contents of the XML file.
  3. Do whatever you want to do with the deserialized stuff.

Here are the classes step 1 will generate for you:

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

And here is how to serialize and deserialize:

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);
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
0

I figured out I could read in the data for the specific tag and put it in a list like this:

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;

    }
jp3434
  • 49
  • 7