Solved

vb.net xml namespace prefixes

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain "Multi-Tenant Services" 5 62
How to use odbc in vb to connect to ms sql 14 34
Animated .jpg? 13 59
Not showing page correctly 3 29
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to dynamically set the form action using jQuery.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now