473,231 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 9264
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: louissan | last post by:
Hi all, I'm coming across a problem, and really do not get where it comes from. The goal: to loop over attributes read from "object" nodes in an imported XML file/flow (via XMLHTTP) and...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
0
by: Maersa | last post by:
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" );...
8
by: fix | last post by:
Hi, I have an XML document and I need to add some attributes to an element. I use the XMLDocument object to load the xml file, and I can get the element I need to add attributes to in an...
6
by: Hans Kamp | last post by:
My program fails reading XML attributes. A fragment of the code is: private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn) { int i; for (i = 0; i < xnl.Count; i++) { XmlNode xn =...
8
by: Ian Collins | last post by:
Is there a way to do this in IE? In Firefox, one can simply append a node from an XML return to the current document. IE whinges about 'No such interface supported' -- Ian Collins.
0
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
1
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
12
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.