Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net xml namespace prefixes

Posted on 2011-02-11
5
Medium Priority
?
1,594 Views
Last Modified: 2013-11-19
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
Comment
Question by:burtrodriguez
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34876110
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
 

Author Comment

by:burtrodriguez
ID: 34876166
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 34876196
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
 

Author Comment

by:burtrodriguez
ID: 34876199
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
 

Author Comment

by:burtrodriguez
ID: 34876267
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question