   [.net] Xml Namespaces Problems
  I'm having some problem with XML namespaces in C#. I'm trying to create a MSNM client, and I'm trying to make the following XML document:&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wssc="http://schemas.xmlsoap.org/ws/2004/04/sc" xmlns:wst="http://schemas.xmlsoap.org/ws/2004/04/trust"&gt;  &lt;Header&gt;    &lt;ps:AuthInfo xmlns:ps="http://schemas.microsoft.com/Passport/SoapServices/PPCRL" Id="PPAuthInfo"&gt; &lt;ps:HostingApp&gt;{7108E71A-9926-4FCB-BCC9-9A9D3F32E423}&lt;/ps:HostingApp&gt; &lt;ps:BinaryVersion&gt;4&lt;/ps:BinaryVersion&gt; &lt;ps:UIVersion&gt;1&lt;/ps:UIVersion&gt; &lt;ps:Cookies&gt;&lt;/ps:Cookies&gt; &lt;ps:RequestParams&gt;AQAAAAIAAABsYwQAAAAzMDg0&lt;/ps:RequestParams&gt;    &lt;/ps:AuthInfo&gt;    &lt;wsse:Security&gt;  &lt;wsse:UsernameToken Id="user"&gt;    &lt;wsse:Username&gt;alice@passport.com&lt;/wsse:Username&gt;&lt;wsse:Password&gt;password&lt;/wsse:Password&gt;  &lt;/wsse:UsernameToken&gt;    &lt;/wsse:Security&gt;  &lt;/Header&gt;&lt;/Envelope&gt;But I can only get it to write the following to appear, even though I put prefixes on the elements...  All the "ps:", and "wsse:"s disappear.&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;Envelope xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wssc="http://schemas.xmlsoap.org/ws/2004/04/sc" xmlns:wst="http://schemas.xmlsoap.org/ws/2004/04/trust" xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt;  &lt;Header xmlns=""&gt;    &lt;AuthInfo xmlns:ps="http://schemas.microsoft.com/Passport/SoapServices/PPCRL" Id="PPAuthInfo"&gt; &lt;HostingApp&gt;{7108E71A-9926-4FCB-BCC9-9A9D3F32E423}&lt;/HostingApp&gt; &lt;BinaryVersion&gt;4&lt;/BinaryVersion&gt; &lt;UIVersion&gt;1&lt;/UIVersion&gt; &lt;Cookies&gt; &lt;/Cookies&gt; &lt;RequestParams&gt;AQAAAAIAAABsYwQAAAAzMDg0&lt;/RequestParams&gt;    &lt;/AuthInfo&gt;    &lt;Security&gt; &lt;UsernameToken Id="user" /&gt;    &lt;/Security&gt;  &lt;/Header&gt;&lt;/Envelope&gt;Does anyone know how to fix this?  My suspicion is that I have to register the namespaces somewhere, because a just added all those to the envelope elements' attribute list using SetAttribute...[Edited by - Axiverse on July 23, 2006 12:05:10 PM]
  So, let me get this straight: are you building the document from scratch by using XmlDocument class? If so, when you create a new element with XmlDocument.CreateElement, you can just give it the fully qualified name like "ps:AuthInfo", and that should theoretically work.Hopefully that helps. Else, maybe a bit more info would be in order. :) Vovan   ;  Yeah, I'm creating it from a XML document class.  I've tried to put "ps:" in the name, but unless I put something in the namespaceURI, it won't appear when I go and save it.   ;  Well, so there's your problem, right? :) Just put the namespace Uri as a parameter, and it will automagically create the attributes where needed, so you don't have to add them by calling SetAttribute.Here's a sample program I wrote:using System;using System.Collections.Generic;using System.Text;using System.Xml;namespace XmlTest{    class Program    {   static void Main(string[] args)   {  string wsseNamespaceUri = "http://schemas.xmlsoap.org/ws/2003/06/secext";  string psNamespaceUri = "http://schemas.microsoft.com/Passport/SoapServices/PPCRL";  XmlDocument doc = new XmlDocument();  XmlNode dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);  doc.AppendChild(dec);  XmlElement envelope = doc.CreateElement("Envelope");  doc.AppendChild(envelope);  XmlElement authInfo = doc.CreateElement("ps", "AuthInfo", psNamespaceUri);  envelope.AppendChild(authInfo);  XmlElement hostingApp = doc.CreateElement("ps", "HostingApp", psNamespaceUri);  hostingApp.InnerText = "{7108E71A-9926-4FCB-BCC9-9A9D3F32E423}";  authInfo.AppendChild(hostingApp);  XmlElement security = doc.CreateElement("wsse", "Security", wsseNamespaceUri);  envelope.AppendChild(security);  XmlElement username = doc.CreateElement("wsse", "Username", wsseNamespaceUri);  username.InnerText = "user";  security.AppendChild(username);  XmlTextWriter w = new XmlTextWriter(Console.OpenStandardOutput(), Encoding.UTF8);  w.Formatting = Formatting.Indented;  doc.Save(w);   }    }}And this here is the output it produces:<?xml version="1.0" encoding="utf-8"?><Envelope>  <ps:AuthInfo xmlns:ps="http://schemas.microsoft.com/Passport/SoapServices/PPCRL">    <ps:HostingApp>{7108E71A-9926-4FCB-BCC9-9A9D3F32E423}</ps:HostingApp>  </ps:AuthInfo>  <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext">    <wsse:Username>user</wsse:Username>  </wsse:Security></Envelope>Looks to me like it is pretty much as desired.Cheers,Vovan   ;  Oh, I see.  Thanks so much!  The thing I didn't know was that you had to put the same namespaceuri for child nodes even if it was already defined in the parent.   