2

I use VS2010 and Fxcop 10.0 (fxcopcmd.exe) programatically for generate fxcop analysis results (xml file)

I would like parser xml file for fxcop analysis results.

In java language I've found this: http://grepcode.com/file/repo1.maven.org/maven2/org.jvnet.hudson.plugins/violations/0.7.7/hudson/plugins/violations/types/fxcop/FxCopParser.java

any suggestions for parser C#?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243

1 Answers1

3

I use this code to get number of issues in the report. You can retrieve actual messages from XElement as well

public class Parser
{
    public Parser(string fileName)
    {
        XDocument doc = XDocument.Load(fileName);
        var issues = GetAllIssues(doc);
        NumberOfIssues = issues.Count;

        var criticalErrors = GetCriticalErrors(issues);
        var errors = GetErrors(issues);
        var criticalWarnings = GetCriticalWarnings(issues);
        var warnings = GetWarnings(issues);

        NumberOfCriticalErrors = criticalErrors.Count;
        NumberOfErrors = errors.Count;
        NumberOfCriticalWarnings = criticalWarnings.Count;
        NumberOfWarnings = warnings.Count;
    }

    public int NumberOfIssues
    {
        get;
        private set;
    }

    public int NumberOfCriticalErrors
    {
        get;
        private set;
    }

    public int NumberOfErrors
    {
        get;
        private set;
    }

    public int NumberOfCriticalWarnings
    {
        get;
        private set;
    }

    public int NumberOfWarnings
    {
        get;
        private set;
    }

    private List<XElement> GetAllIssues(XDocument doc)
    {
        IEnumerable<XElement> issues =
            from el in doc.Descendants("Issue")
            select el;

        return issues.ToList();
    }

    private List<XElement> GetCriticalErrors(List<XElement> issues)
    {
        IEnumerable<XElement> errors = 
            from el in issues
            where (string)el.Attribute("Level") == "CriticalError"
            select el;

        return errors.ToList();
    }

    private List<XElement> GetErrors(List<XElement> issues)
    {
        IEnumerable<XElement> errors =
            from el in issues
            where (string)el.Attribute("Level") == "Error"
            select el;

        return errors.ToList();
    }

    private List<XElement> GetCriticalWarnings(List<XElement> issues)
    {
        IEnumerable<XElement> warn =
            from el in issues
            where (string)el.Attribute("Level") == "CriticalWarning"
            select el;

        return warn.ToList();
    }

    private List<XElement> GetWarnings(List<XElement> issues)
    {
        IEnumerable<XElement> warn =
            from el in issues
            where (string)el.Attribute("Level") == "Warning"
            select el;

        return warn.ToList();
    }
}
Evgeny78
  • 31
  • 3
  • I've used your code to fix https://stackoverflow.com/questions/56195786/jenkins-fxcop-errors-should-fail-the-build/56195787#56195787 – Anuja Lamahewa May 18 '19 at 03:46