I need to be able to define an attribute with a prefix in a xml element.
For instance…
<nc:Person s:id="ID_Person_01"></nc:Person>
In order to do this I though that the following would have worked.
XmlElement TempElement = XmlDocToRef.CreateElement("nc:Person", "http://niem.gov/niem/niem-core/2.0");
TempElement.SetAttribute("s:id", "http://niem.gov/niem/structures/2.0", "ID_Person_01");
Unfortunately, XmlElement.SetAttribute(string, string, string) does not seem to support parsing the prefix as I receive the error below.
The ‘:’ character, hexadecimal value 0x3A, cannot be included in a name.
How would I define an attribute with prefix?
If you’ve already declared your namespace in the root node, you just need to change the
SetAttributecall to use the unprefixed attribute name. So if your root node defines a namespace like this:You can do this and the attribute will pick up the prefix you’ve already established:
If you have not yet declared the namespace (and its prefix), the three-string
XmlDocument.CreateAttributeoverload will do it for you: