I'm not sure what you mean when you say that this works at design time. Are
you saying your code compiles? That's no indication if your code being
semantically correct.
Do you really have to add the namespace on EACH element because they belong
to different namespaces or is it sufficient to just declare it at the root
level?
If your <root> element also belongs to your target namespace, then you could
simply do:
XmlElement elem = doc.CreateElement( "root", "urn:mynamespace" );
If your root doesn't belong to the namespace, but all XML content below
<root> do, then it's still sufficient to put a default namespace declaration
at the first XML element child of root.
Take a look at error message in the exception that's happening. It will tell
you why your code isn't working.
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
[quoted text, click to view] "Maersa" <ma_ersa@hotmail.com> wrote in message
news:eIADxegSEHA.3140@tk2msftngp13.phx.gbl...
> hi,
>
> i'm trying to add "attributes" to some nodes and it works at design time
> but
> fails at runtime.
>
> XmlDocument doc = new XmlDocument();
>
> XmlElement elem = doc.CreateElement( "root" );
> elem.InnerXml = "<first>abcde</first><last>1234</last>";
>
> Now, what I'd like to do here is, go through adding "namespaces" to the
> child nodes of <root>.
>
> XmlNode node = elem.FirstChild;
>
> while( node != null )
> {
> XmlAttribute attr = node.OwnerDocument.CreateAttribute( "xmlns" );
> attr.Value = "urn:mynamespace";
> node.Attributes.Append( attr ); // This works in designtime but
> fails at runtime
> node = node.NextSibiling;
> }
>
> Is there some better way in setting the namespace for EACH node element ?
> The xsd
> requires that i have a namespace on each node.
>
> thanks,
>
>