Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1601
  • Last Modified:

vb.net xml namespace prefixes

This, to me, seems like a related question...along the lines of xml namespaces and their respective prefixes.  Let me see if I can explain matters clear enough for you guys/gals to be able to help me out since this has been plaguing me for a long long time.

I have an xml document w/ one namespace whose shortcut abbreviation is 'ns'.  Now this xml document is really just the skeleton or template of the xml layout.  Our aim, most of the time,  is to add in the values, for example <ns:firstname></ns:firstname> will end up being <ns:firstname>JOHN</ns:firstname>.

First off, since there's only one namespace being used...is it "wrong" to prefix every element in the xml document with the prefix 'ns:'??  Is there any advantage to this?  I'm sure if I ask my boss he will say that in the future there could be another namespace being used...but after a few years, this hasn't changed!

The real problem though is when I have to add in another xml element.  So let's say I have to add in a shoe size element, <ns:shoeSize></ns:showSize>.  Yes it has to be added in, it can't be just filled in with a value because it's presence is optional.  For the life of me I can't figure out how to make the vb.net code add in the element WITH the 'ns:' prefix.

Please help.
0
burtrodriguez
Asked:
burtrodriguez
  • 3
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
How are you currently adding the new node? You should be able to specify the namespace for the node when you add it to the document.
0
 
burtrodriguezAuthor Commented:
I create the node as follows:
Dim xmlQueue As XmlElement = _xMAXeRelease.CreateElement("ns:queue")

I then proceed to build it out by adding some Attributes to it.  And lastly, I then add it to my xml document as follows:

xmlQueues.AppendChild(xmlQueue)

but when I then open up the xml file I only see <queue> and not <ns:queue> as I want to.

Thank you Carl.
0
 
Carl TawnSystems and Integration DeveloperCommented:
You need to use the overload for CreateElement() and specify the namespace that the node should belong to.

i.e.
Dim xmlQueue As XmlElement = _xMAXeRelease.CreateElement("ns:queue", "your_namespace")

Open in new window

0
 
burtrodriguezAuthor Commented:
Correct me if I am wrong but I think the following works.  I need to test it out further to be 100% certain.  I'm in disbelief it could be this easy since this has diverted me, on several occasions for a year and half or two, to fake the XML and treat it as a string.

_xMAXeRelease.CreateElement("ns:queue", "expanded namespace for the prefix")

??

0
 
burtrodriguezAuthor Commented:
Carl, thank you so much.  I figured this out just as you were posting you answer, but I hadn't refreshed...was searching the internet; determined to not cower away yet again from this.  I will still gladly award you the points without any hesitation since having this/your solution means I can now go and enjoy my Friday evening!  

Mind you maybe not my entire weekend but at least tonight.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now