Solved

vb.net xml namespace prefixes

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

11 Experts available now in Live!

Get 1:1 Help Now