Solved

vb.net xml namespace prefixes

Posted on 2011-02-11
5
1,539 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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

830 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