Paul Selles
Computers and cats
Tag Archives: Visual Studio
XAML Formatting: Programmatically Tabify XAML and XML Files in C#
April 2, 2014
Posted by on With many developers working together, formatting styles can sometimes be an issue. This is most noticeable when working with XAML files, and the biggest culprit for formatting issues is poor tabbing and inconsistent tab characters. Luckily there is an easy way to standardize XAML formatting through that implantation of a custom check-in policy that can programmatically fix all XAML files prior to being checked in.
Using XmlWriter in conjunction with the XmlWriterSettings class we can easily customize our output [1][2]. The XmlWriterSettings properties give us a lot of control as to how we want the XML or XAML to look and our settings are documented and are worth looking over. I am also using XmlReader and XmlReaderSettings to convert the text into an XmlDocument, were the XmlReaderSettings class is used to ensure that we ignore any potentially invalid XML characters in our XAML [3][4][5][6]. I encountered one tricky bit were the XmlReader will break on decimal and hex character reference, this was solved by doing a text replace on all “&” with “&” pre-XmlReader and converting back post-XmlWriter.
public static class Tabify { // Tabify XML document public static void Xml(string filename) { DoTabify(filename, false); } // Tabify Xaml document public static void Xaml(string filename) { DoTabify(filename, true); } // Tabify private static void DoTabify(string filename, bool xaml=false) { // XmlDocument container XmlDocument xmlDocument = new XmlDocument(); // We want to make sure that decimal and hex character references are not lost string xmlString = File.ReadAllText(filename); xmlString = xmlString.Replace("&", "&"); // Xml Reader settings XmlReaderSettings xmlReadSettings = new XmlReaderSettings() { CheckCharacters = false, // We have some invalid characters we want to ignore }; // Use XML reader to load content to XmlDocument container using (XmlReader xmReader = XmlReader.Create(new StringReader(xmlString), xmlReadSettings)) { xmReader.MoveToContent(); xmlDocument.Load(xmReader); } // Customize how our XML will look, we want tabs, UTF8 encoding and new line on attributes XmlWriterSettings xmlWriterSettings = new XmlWriterSettings() { Indent = true, // Indent elements IndentChars = "\t", // Indent with tabs CheckCharacters = false, // Ignore invalid characters NewLineChars = Environment.NewLine, // Set newline character NewLineHandling = NewLineHandling.None, // Normalize line breaks Encoding = new UTF8Encoding() // UTF8 encoding }; // We do not want the xml declaration for xaml files if (xaml) xmlWriterSettings.OmitXmlDeclaration = true; // For XAML this must be false!!!! StringBuilder xmlStringBuilder = new StringBuilder(); // Write xml to file using saved settings using (XmlWriter xmlWriter = XmlWriter.Create(xmlStringBuilder, xmlWriterSettings)) { xmlWriter.Flush(); xmlDocument.WriteContentTo(xmlWriter); } // Restore decimal and hex character references xmlString = xmlStringBuilder.ToString().Replace("&", "&"); File.WriteAllText(filename, xmlString); } }
Paul
References
[1] XmlWriter Class. MSDN Library.
[2] XmlWriterSettings Class. MSDN Library.
[3] XmlReader ClassXmlReader Class. MSDN Library.
[4] XmlReaderSettings Class. MSDN Library.
[5] XmlDocument Class. MSDN Library.
[6] Parsing Xml with Invalid Characters in C#. Paul Selles
TFS API: TFS User Email Address Lookup and Reverse Lookup
March 24, 2014
Posted by on Occasionally I need to develop a tool that requires sending and receiving emails to and from developers. To do this I will need to lookup email addresses based on a TFS user’s display or account name and vice versa. Lucky for us this is painfully easy to do using IIdentityManagementService.ReadIdentity and the TeamFoundationIdentity class [1][2]:
private static readonly string TeamProjectUriString = "http://tfs.yourtfsurl.com"; // Team Project Collection getter private static TfsTeamProjectCollection _tfsTeamProjectCollection; public static TfsTeamProjectCollection TfsTeamProjectCollection { get { return _tfsTeamProjectCollection ?? (_tfsTeamProjectCollection = new TfsTeamProjectCollection(new Uri(TeamProjectUriString))); } } // Identity Management Service getter private static IIdentityManagementService _identityManagementService; public static IIdentityManagementService IdentityManagementService { get { return _identityManagementService ?? (_identityManagementService = TfsTeamProjectCollection.GetService<IIdentityManagementService>()); } } // Get Email Address from TFS Account or Display Name public static string GetEmailAddress(string userName) { TeamFoundationIdentity teamFoundationIdentity = IdentityManagementService.ReadIdentity( IdentitySearchFactor.AccountName | IdentitySearchFactor.DisplayName, userName, MembershipQuery.None, ReadIdentityOptions.None); return teamFoundationIdentity.GetAttribute("Mail", null); } // Get TFS Display Name from and Email Address public static string GetDisplayName(string emailAddress) { TeamFoundationIdentity teamFoundationIdentity = IdentityManagementService.ReadIdentity( IdentitySearchFactor.MailAddress, emailAddress, MembershipQuery.None, ReadIdentityOptions.None); return teamFoundationIdentity.DisplayName; }
References
[1] IIdentityManagementService.ReadIdentity Method. MSDN
[2] TeamFoundationIdentity Class. MSDN
VS2013: Opening Build In Browser
March 6, 2014
Posted by on TFS 2012: WorkItemChangedEventHandler and where is WorkItemChangedEvent
December 4, 2013
Posted by on I am creating a custom action for our TFS2012 Work Item for State Transitions. I found a multitude of resources on how the get the ball rolling create a WorkItemChangedEventHandler class [1]; but I quickly hit a snag. I could not find WorkItemChangedEvent for the life of me. I was expecting to find WorkItemChangedEvent in the assembly Microsoft.TeamFoundation.WorkItemTracking.Server but due to a poorly documented reason there are some dependencies with the assembly Microsoft.TeamFoundation.WorkItemTracking.Server.Dataaccesslayer. Both assemblies must be present to access any members of Microsoft.TeamFoundation.WorkItemTracking.Server.
Both of these assemblies can be found on the TFS2012 Server machine under the path %ProgramFiles%\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin.
Reference
[1] Jakob Ehn. Developing and debugging Server Side Event Handlers in TFS 2010. 23-Jan-2012
Recent Comments