Pergunta

Alguém sabe de uma maneira de ler programaticamente a lista de referências em um arquivo VS2008 CSPROJ? O MSBuild não parece suportar essa funcionalidade. Estou tentando ler os nós carregando o arquivo CSPROJ em um XMLDocument, mas a pesquisa XPath não retorna nenhum nós. Estou usando o seguinte código:

System.Xml.XmlDocument projDefinition = new System.Xml.XmlDocument();
        projDefinition.Load(fullProjectPath);

        System.Xml.XPath.XPathNavigator navigator = projDefinition.CreateNavigator();

        System.Xml.XPath.XPathNodeIterator iterator = navigator.Select(@"/Project/ItemGroup");
        while (iterator.MoveNext())
        {
            Console.WriteLine(iterator.Current.Name);
        }

Se eu conseguir obter a lista de grupos de item, posso determinar se ele contém informações de referência ou não.

Foi útil?

Solução

O XPath deveria ser /Project/ItemGroup/Reference, e você esqueceu o espaço para nome. Eu apenas usaria o Xlinq - lidando com namespaces em XPathNavigator é bastante confuso. Então:

    XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
    XDocument projDefinition = XDocument.Load(fullProjectPath);
    IEnumerable<string> references = projDefinition
        .Element(msbuild + "Project")
        .Elements(msbuild + "ItemGroup")
        .Elements(msbuild + "Reference")
        .Select(refElem => refElem.Value);
    foreach (string reference in references)
    {
        Console.WriteLine(reference);
    }

Outras dicas

Com base na resposta de @Pavel Minaev, foi isso que funcionou para mim (observe a linha .Attributes adicionada para ler o atributo de inclusão)

XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
    XDocument projDefinition = XDocument.Load(@"D:\SomeProject.csproj");
    IEnumerable<string> references = projDefinition
        .Element(msbuild + "Project")
        .Elements(msbuild + "ItemGroup")
        .Elements(msbuild + "Reference")
        .Attributes("Include")    // This is where the reference is mentioned       
        .Select(refElem => refElem.Value);
    foreach (string reference in references)
    {
        Console.WriteLine(reference);
    }

Com base na resposta do @Pavelminaev, também adicionei o elemento "Hintpath" à saída. Escrevo a matriz da string "Referências" para um arquivo ".txt".

XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
            XDocument projDefinition = XDocument.Load(@"C:\DynamicsFieldsSite.csproj");
            var references = projDefinition
                .Element(msbuild + "Project")
                .Elements(msbuild + "ItemGroup")
                .Elements(msbuild + "Reference")
                .Select(refElem => (refElem.Attribute("Include") == null ? "" : refElem.Attribute("Include").Value) + "\n" + (refElem.Element(msbuild + "HintPath") == null ? "" : refElem.Element(msbuild + "HintPath").Value) + "\n");
            File.WriteAllLines(@"C:\References.txt", references);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top