public abstract LookupPrefix ( string ns ) : string | ||
ns | string | |
return | string |
XmlWriterSettings settings = new XmlWriterSettings(); settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; using (XmlWriter writer = XmlWriter.Create("output.xml", settings)) { writer.WriteStartElement("root"); writer.WriteAttributeString("xmlns", "ns1", null, "http://www.example.com/ns1"); writer.WriteStartElement("child", "http://www.example.com/ns1"); string prefix = writer.LookupPrefix("http://www.example.com/ns1"); Console.WriteLine(prefix); // Output: ns1 // Write child elements and attributes writer.WriteEndElement(); writer.WriteEndElement(); }
XmlWriterSettings settings = new XmlWriterSettings(); settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; using (XmlWriter writer = XmlWriter.Create("output.xml", settings)) { writer.WriteStartElement("root"); writer.WriteAttributeString("xmlns", null, null, "http://www.example.com/ns1"); string prefix = writer.LookupPrefix("http://www.example.com/ns1"); Console.WriteLine(prefix); // Output: "" // Write child elements and attributes writer.WriteEndElement(); }This code snippet creates an XML document with a root element and a default namespace declaration. It uses LookupPrefix method to retrieve the prefix associated with the namespace URI "http://www.example.com/ns1", which will be an empty string since there is no prefix associated with the default namespace. Package library: System.Xml