XElement element = XElement.Load("example.xml"); XNamespace ns = "http://www.example.com"; string prefix = element.GetPrefixOfNamespace(ns); Console.WriteLine($"Prefix for '{ns}' is '{prefix}'");
XElement element = new XElement("example", "text"); XNamespace ns = "http://www.example.com"; element.Add(new XAttribute(XNamespace.Xmlns + "ex", ns)); string prefix = element.GetPrefixOfNamespace(ns); Console.WriteLine($"Prefix for '{ns}' is '{prefix}'");This code creates a new XElement with an associated XML namespace and prefix, and retrieves the XML namespace prefix associated with the same namespace URL. The prefix, if any, is then printed to the console. The package library for System.Xml.Linq is included with the .NET framework and provides classes for working with XML data.