2012-10-08 47 views
0

我想寫一個簡單的程序來查詢TFS和所有的工作項目轉換成統一的類型的XML文件並保存到一個文件夾在不同的文件。將TFS工作項目轉換爲XML的示例代碼?

我敢肯定這樣的工作通常是做得不夠,而且是非常簡單的 - 但我能找到在互聯網上沒有樣品,沒有連接到TFS編程和檢索唯一的工作項目信息的方式。任何人都可以幫助我嗎?

非常感謝

回答

1
private TfsTeamProjectCollection GetTfsTeamProjectCollection() 
    { 
     TeamProjectPicker workitemPicker = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false, new UICredentialsProvider()); 
     workitemPicker.AcceptButtonText = "workitemPicker.AcceptButtonText"; 
     workitemPicker.Text = "workitemPicker.Text"; 
     workitemPicker.ShowDialog(); 
     if (workitemPicker.SelectedProjects != null || workitemPicker.SelectedProjects.Length > 0) 
     { 
      return workitemPicker.SelectedTeamProjectCollection; 
     } 
     return null; 
    } 

    private WorkItemCollection WorkItemByQuery(TfsTeamProjectCollection projects, string query) //query is likethis:SELECT [System.ID], [System.Title] FROM WorkItems WHERE [System.Title] CONTAINS 'Lei Yang' 
    { 
     WorkItemStore wis = new WorkItemStore(projects); 
     return wis.Query (query); 
    } 

WorkItemCollection是你想要的。您可以獲得WorkItems及其屬性。

1

您應該使用TFS SDK。

你可以找到這樣one網上很多教程。

MSDN也會幫助你。

0

由楊雷的建議,您可以獲取查詢結果。 然後開始構建XML。

XmlDocument xmlDoc = new XmlDocument(); 
//XML declaration 
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); 
// Create the root element 
XmlElement rootNode = xmlDoc.CreateElement("WorkItemFieldList"); 
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement); 
xmlDoc.AppendChild(rootNode); 
//Create a new element and add it to the root node 
XmlElement parentnode = xmlDoc.CreateElement("UserInput"); 

迭代通過所有的工作項字段值

xmlDoc.DocumentElement.PrependChild(parentnode); 
//wiTrees of type WorkItemLinkInfo[] is the result of RunLinkQuery 
foreach (var item in wiTrees) 
{ 
int fieldcount = workItemStore.GetWorkItem(item.TargetId).Fields.Count; 
while (fieldcount > 0) 
{ 
//Create the required nodes 
XmlElement mainNode = xmlDoc.CreateElement(workItemStore.GetWorkItem(item.TargetId).Fields[fieldcount -1].Name.ToString().Replace(" ", "-")); 
// retrieve the text 
//Use the custom method NullSafeToString to handle null values and convert them to String.Empty 
XmlText categoryText = xmlDoc.CreateTextNode(workItemStore.GetWorkItem(item.TargetId).Fields[fieldcount - 1].Value.NullSafeToString().ToString()); 
// append the nodes to the parentNode without the value 
parentnode.AppendChild(mainNode); 
// save the value of the fields into the nodes 
mainNode.AppendChild(categoryText); 
fieldcount--; 
} 

} 
// Save to the XML file 
xmlDoc.Save("widetails.xml");