Parser FxCop Results Xml file in C#

半腔热情 提交于 2020-01-04 14:19:46

问题


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#?


回答1:


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


来源:https://stackoverflow.com/questions/14477933/parser-fxcop-results-xml-file-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!