create XML element with namespace

MikeLI

Member
Joined
Dec 28, 2005
Messages
8
Programming Experience
Beginner
Hi All,

I have a function. It creates an XML element:

VB.NET:
[FONT=Courier New]Function newElement(ByVal doc As XmlDocument, [/FONT]
[FONT=Courier New]                  ByVal name As String, _ [/FONT]
[FONT=Courier New]         Optional ByVal text As String = Nothing, _ [/FONT]
[FONT=Courier New]         Optional ByVal pref As String = Nothing) As XmlElement [/FONT]
[FONT=Courier New]  Dim xmlEl As XmlElement = doc.CreateElement(pre, name, "") [/FONT]
[FONT=Courier New]  If Not (text Is Nothing) Then xmlEl.InnerText = text [/FONT]
[FONT=Courier New]  doc.DocumentElement.AppendChild(xmlEl) [/FONT]
[FONT=Courier New]  Return xmlEl[/FONT]
[FONT=Courier New]End Function[/FONT]

Then, I call that function in this way:

newElement(xmlDoc, "FirstName", "Peter", "pref")

I expect the result:
<pref:FirstName>Peter</FirstName>

However, the result has no "pref:", just:
<FirstName>Peter</FirstName>


Why?????


Mike
 
If you want to create an XML element with namespace you can do that as it follows:
VB.NET:
[SIZE=2]xmlWriter.WriteStartElement("r", "FirstName", "urn:namespaceURI")
[/SIZE][SIZE=2][SIZE=2]xmlWriter.WriteString("Kulrom")
xmlWriter.WriteEndElement()
[/SIZE][/SIZE]


output will look like:

VB.NET:
<r:FirstName xmlns:r="urn:namespaceURI">Kulrom</r:FirstName> 
[SIZE=2][SIZE=2]
[/SIZE][/SIZE]


Regards ;)

EDIT: btw, why you use prefix? If you want to use it for the firstName element only then you don't need that. .
I am usually using prefix when i want to have the same element in a two documents and then i use prefix for Solving Name Conflicts :)
 
Last edited:
MikeLI, You didn't specify a NamespaceURI (the pre/pref spelling error did not matter in context, but I guess that is was an error with your post). If an attribute is not explicitly given a namespace, it simply has no namespace. So if you give this information the prefix will be added and the prefix namespace is defined as an attribute to the node. Example:
VB.NET:
Sub xml()
  Dim xdoc As New Xml.XmlDocument
  Dim xe As Xml.XmlElement
'declaration
  xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "UTF-8", ""))
'documentelement
[SIZE=2][COLOR=black]  xe = xdoc.CreateElement([/COLOR][/SIZE][SIZE=2][COLOR=black]"root"[/COLOR][/SIZE][SIZE=2][COLOR=black])[/COLOR]
[/SIZE]  xdoc.AppendChild(xe)
'node1
  xe = xdoc.CreateElement("pre", "nodename", "http://bogus") 
  xe.InnerText = "nodetext1"
  xdoc.DocumentElement.AppendChild(xe)
'node2
  xe = xdoc.CreateElement("pre", "nodename", "http://bogus") 
  xe.InnerText = "nodetext2"
  xdoc.DocumentElement.AppendChild(xe)
'node3
  xe = xdoc.CreateElement("pre", "nodename", "http://bogus") 
  xe.InnerText = "nodetext3"
  xdoc.DocumentElement.AppendChild(xe)
'review
  MsgBox(xdoc.OuterXml)
End Sub
output is, you see the namespace prefix is defined privately each node:
HTML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<pre:nodename xmlns:pre="http://bogus">nodetext</pre:nodename>
<pre:nodename xmlns:pre="http://bogus">nodetext2</pre:nodename>
<pre:nodename xmlns:pre="http://bogus">nodetext3</pre:nodename>
</root>
It is even better to specify the prefix at a top level node, think of it as a shortcut definition for the prefix. Example:
VB.NET:
Sub xml()
  Dim xdoc As New Xml.XmlDocument
  Dim xe As Xml.XmlElement
'declaration
  xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "UTF-8", ""))
'documentelement
[SIZE=2][COLOR=black]  xe = xdoc.CreateElement([/COLOR][/SIZE][SIZE=2][COLOR=black]"root"[/COLOR][/SIZE][SIZE=2][COLOR=black])[/COLOR]
[/SIZE][COLOR=black]  xdoc.AppendChild(xe)[/COLOR]
'prefix shortcut
[SIZE=2][COLOR=black]  Dim xa As Xml.XmlAttribute[/COLOR]
[/SIZE][SIZE=2][COLOR=black]  xa = xdoc.CreateAttribute("xmlns", "pre", "http://www.w3.org/2000/xmlns/")[/COLOR]
[/SIZE][SIZE=2][COLOR=black]  xa.InnerText = "http://bogus"[/COLOR]
[/SIZE][SIZE=2][COLOR=black]  xe.Attributes.Append(xa)[/COLOR][/SIZE]
[COLOR=black]'lookup namespace[/COLOR]
[SIZE=2][COLOR=black]  Dim[/COLOR][/SIZE][COLOR=black][SIZE=2] ns [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2] = xdoc.DocumentElement.GetNamespaceOfPrefix([/SIZE][SIZE=2]"pre"[/SIZE][SIZE=2])
[/SIZE][/COLOR][SIZE=2][COLOR=black]'node1[/COLOR]
[/SIZE][COLOR=black]  xe = xdoc.CreateElement("pre", "nodename", [/COLOR][COLOR=black]ns[/COLOR][COLOR=black])[/COLOR]
  xe.InnerText = "nodetext1"
  xdoc.DocumentElement.AppendChild(xe)
'node2
[COLOR=black]  xe = xdoc.CreateElement("pre", "nodename", [/COLOR][COLOR=black]ns[/COLOR][COLOR=black])[/COLOR]
  xe.InnerText = "nodetext2"
  xdoc.DocumentElement.AppendChild(xe)
'node3
[COLOR=black]  xe = xdoc.CreateElement("pre", "nodename", [/COLOR][COLOR=black]ns[/COLOR][COLOR=black])[/COLOR]
  xe.InnerText = "nodetext3"
  xdoc.DocumentElement.AppendChild(xe)
'review
  MsgBox(xdoc.OuterXml)
End Sub
out is then, and you see namespace prefix is defined at root node:
HTML:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:pre="http://bogus">
<pre:nodename>nodetext</pre:nodename>
<pre:nodename>nodetext2</pre:nodename>
<pre:nodename>nodetext3</pre:nodename>
</root>
I deliberately typed out the three child node creations without using a custom function method like you did, because there are many optionals and checks that could confuse the basic process, so for this example code was made simple. Also in this case I would have chosen to go for method overloading instead of optional parameters, but it's perhaps just a preference.
 
Back
Top