How to XSL transform a XDocument in C# and VB.NET with custom namespaces

This snippet will help you to xsl transform xml files with custom namespaces. A Basic snippet which does only work without custom namespaces can be found HERE. The snippet will return a path to a temporary file (transformed xml).

Sample C#

private static string TransformXDocument(string inputXml, string xslFile)
        {
            try
            {
                var xslt = new XslCompiledTransform();
                xslt.Load(xslFile);
                string tempFile = string.Empty;
                using (var sr = new StreamReader(inputXml, Encoding.Default))
                {
                    var xDoc = XDocument.Load(xslFile);
                    if (xDoc.Root != null)
                    {
                        var result = xDoc.Root.Attributes().Where(a => a.IsNamespaceDeclaration).GroupBy(a => a.Name.Namespace == XNamespace.None ? String.Empty : a.Name.LocalName, a => XNamespace.Get(a.Value)).ToDictionary(g => g.Key,g => g.First());
                        var nt = new NameTable();
                        var mgr = new XmlNamespaceManager(nt);
                        foreach (var ns in result.Where(ns => ns.Key != "xsl" && ns.Value != "http://www.w3.org/1999/XSL/Transform"))
                        {
                            mgr.AddNamespace(ns.Key, ns.Value.ToString());
                        }
                        var xpc = new XmlParserContext(nt, mgr, "", XmlSpace.Default);
                        var rds = new XmlReaderSettings {ConformanceLevel = ConformanceLevel.Document};
                        using (var rd = XmlReader.Create(sr, rds, xpc))
                        {
                            tempFile = Path.GetTempFileName();
                            using (var wr = new StreamWriter(tempFile))
                            {
                                xslt.Transform(rd, new XsltArgumentList(), wr);
                                wr.Flush();
                                wr.Close();
                            }
                            rd.Close();
                        }
                    }
                }
                return tempFile;

            }
            catch (Exception ex)
            {
                //handle the exception your way
                return string.Empty;
            }
        }

Sample VB.NET

Private Shared Function TransformXDocument(inputXml As String, xslFile As String) As String
	Try
		Dim xslt = New XslCompiledTransform()
		xslt.Load(xslFile)
		Dim tempFile As String = String.Empty
		Using sr = New StreamReader(inputXml, Encoding.Default)
			Dim xDoc = XDocument.Load(xslFile)
			If xDoc.Root IsNot Nothing Then
				Dim result = xDoc.Root.Attributes().Where(Function(a) a.IsNamespaceDeclaration).GroupBy(Function(a) If(a.Name.[Namespace] = XNamespace.None, [String].Empty, a.Name.LocalName), Function(a) XNamespace.[Get](a.Value)).ToDictionary(Function(g) g.Key, Function(g) g.First())
				Dim nt = New NameTable()
				Dim mgr = New XmlNamespaceManager(nt)
				For Each ns As var In result.Where(Function(ns) ns.Key <> "xsl" AndAlso ns.Value <> "http://www.w3.org/1999/XSL/Transform")
					mgr.AddNamespace(ns.Key, ns.Value.ToString())
				Next
				Dim xpc = New XmlParserContext(nt, mgr, "", XmlSpace.[Default])
				Dim rds = New XmlReaderSettings() With { _
					Key .ConformanceLevel = ConformanceLevel.Document _
				}
				Using rd = XmlReader.Create(sr, rds, xpc)
					tempFile = Path.GetTempFileName()
					Using wr = New StreamWriter(tempFile)
						xslt.Transform(rd, New XsltArgumentList(), wr)
						wr.Flush()
						wr.Close()
					End Using
					rd.Close()
				End Using
			End If
		End Using

		Return tempFile
	Catch ex As Exception
		'handle the exception your way
		Return String.Empty
	End Try
End Function

Please rate this snippet

How to open a Website in the default Browser in C# and VB.NET

To open a Website in the default Browser in C# and VB.NET you can use the snippet below.

Sample C#

System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/");

Sample VB

System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/")

Please rate this snippet

How to change default Admin Username in WordPress

Every default WordPress installation will create an account with a default Admin username. This can be a security issue.
To change default Admin Username in WordPress you can use the mySql snippet below.

UPDATE wp_users SET user_login = 'NewUsername' WHERE user_login = 'Admin';

Please rate this snippet

How to get the default printer name in C# and VB.NET

To get the default printer name in C# and VB.NET you can use the following snippet.

Samples

Sample C#

Sample VB.NET

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet