2009-07-27 23 views
24

Có ai biết cách lập trình đọc danh sách Tài liệu tham khảo trong tệp csproj VS2008 không? MSBuild không hỗ trợ chức năng này. Tôi đang cố đọc các nút bằng cách tải tệp csproj vào XmlDocument nhưng, tìm kiếm XPath không trả về bất kỳ nút nào. Tôi đang sử dụng mã sau:Đọc danh sách Tài liệu tham khảo từ các tệp csproj

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

Nếu tôi có thể nhận danh sách các Nhóm sản phẩm, tôi có thể xác định xem nó có chứa thông tin tham chiếu hay không.

Trả lời

37

XPath phải là /Project/ItemGroup/Reference và bạn đã quên không gian tên. Tôi chỉ sử dụng XLINQ - xử lý các không gian tên trong XPathNavigator là khá lộn xộn. Vì vậy:

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); 
    } 
+0

Điều đó dễ hơn nhiều. Cảm ơn đã giúp đỡ. –

+0

Điều này thật tuyệt! Bởi bây giờ mọi người có thể nhận thấy nó, nhưng chỉ trong trường hợp - tài liệu tham khảo cũng có thể được thực hiện trong các giải pháp, trong trường hợp này bạn cần phải nhận được 'ProjectReference' yếu tố quá. – astrowalker

6

Xây dựng về câu trả lời @Pavel Minaev, đây là những gì làm việc cho tôi (chú ý dòng .Attributes thêm để đọc các thuộc tính Include)

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

Dựa trên câu trả lời @ PavelMinaev, tôi cũng đã thêm phần tử "HintPath" vào đầu ra. Tôi viết chuỗi "tham chiếu" vào một tập tin ".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); 
Các vấn đề liên quan