C# 将TFS工作项转换为XML的示例代码?

C# 将TFS工作项转换为XML的示例代码?,c#,.net,xml,visual-studio-2010,tfs,C#,.net,Xml,Visual Studio 2010,Tfs,我想编写一个简单的程序来查询TFS,并将所有工作项转换为统一类型的XML文件,并将它们保存到文件夹中的单独文件中 我相信这类工作通常已经足够了,而且非常简单——但我在Internet上找不到任何示例,也找不到以编程方式连接到TFS并仅检索工作项信息的方法。有人能帮我吗 非常感谢您应该使用TFS SDK 你可以在网上找到很多这样的教程 也会对您有所帮助。您应该使用TFS SDK 你可以在网上找到很多这样的教程 也将对您有所帮助。您可以使用“与一起工作”。然后,您所需要做的就是将其转换为所需的格式。

我想编写一个简单的程序来查询TFS,并将所有工作项转换为统一类型的XML文件,并将它们保存到文件夹中的单独文件中

我相信这类工作通常已经足够了,而且非常简单——但我在Internet上找不到任何示例,也找不到以编程方式连接到TFS并仅检索工作项信息的方法。有人能帮我吗


非常感谢

您应该使用TFS SDK

你可以在网上找到很多这样的教程


也会对您有所帮助。

您应该使用TFS SDK

你可以在网上找到很多这样的教程

也将对您有所帮助。

您可以使用“与一起工作”。然后,您所需要做的就是将其转换为所需的格式。

您可以使用working with。然后你需要做的就是把它转换成你想要的格式

    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 );
    }
工作项集合就是您想要的。您可以获取工作项及其属性


工作项集合就是您想要的。您可以获取工作项及其属性。

您可以按照雷阳的建议获取查询结果。 然后开始构建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");
遍历所有workitem字段值

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

您可以按照雷阳的建议获取查询结果。 然后开始构建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");
遍历所有workitem字段值

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