469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Adding attributes to nodes...

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,
Nov 12 '05 #1
2 9118
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

"Maersa" <ma*****@hotmail.com> wrote in message
news:eI**************@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,

Nov 12 '05 #2
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

"Maersa" <ma*****@hotmail.com> wrote in message
news:eI**************@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,

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Maersa | last post: by
8 posts views Thread by fix | last post: by
6 posts views Thread by Hans Kamp | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.