Invalid Xml Namespace Error

I am trying to construct an XML file and running into namespace issues when trying to set an attribute. I dwindled it down to this simple example:

  Dim xml As New XmlDocument
  Dim x As XmlElement = xml.CreateElement("http://localhost/abc", "x:doc")
  xml.AppendChild x
  
  Dim y As XmlElement = xml.CreateElement("x:another")
  y.SetAttribute("x:name", "John")
y.SetAttribute

Notice I am using the namespace “x” in the tag names w/o an issue, but I can’t in the attributes.

Any thoughts?

I got it. The proper way to do this, I believe, is:

Const kUri = "http://localhost/abc"

Dim x As XmlElement = xml.CreateElement(kUri, "x:doc")
xml.AppendChild x
  
Dim y As XmlElement = xml.CreateElement(kUri, "x:another")
y.SetAttribute(kUri, "x:name", "John")

This obviously is a very narrowed down snippet that showed my error and correction.

1 Like