XMLNode add attribute without prefix

I have the following method:

XmlNode AttributeNode = xmlDocument.CreateNode(XmlNodeType.Attribute, fieldName, WILTSNAMESPACE);
		AttributeNode.Value = fieldValue;

this create (for example):

<Person d2p1:PersonRole=“Boss”>

How do I add an attribute without the d2p1: prefix:

<Person PersonRole=“Boss”>


You need to add a schema and bind that attribute to said schema. The namespace prefix is actually unimportant, at least with proper parsers.

Hum. Not so sure about that. You really do not need a schema to control namespaces.

The problem above is that the element name resides in another namespace (the default?) than that of the attribute. In that case the attribute absolutely must be qualified by a namespace qualifier. And a proper parser will [i]definitively[/] demand that the attribute resides in the correct namespace.

I suspect that boxheads problem run a little deeper. He/she needs to find out what namespace the element name and attribute name must reside in respectively. If the two resides in the same namespace then the xml writer will not qualify the attribute with a namespace qualifier.

So the solution may be to assign the same namespace to the element name. But as I said, I suspect that boxhead needs to find out exactly what will be expected when the xml is read.

^^^^Yes, that too. Probably would help if he could post a schema . . .

OK - you just said a whole bunch of stuff that’s new to me!

I thought xml did not need to include namespaces?

I am creating two different lots of XML - one will be used by our external website, which is written in PHP inhouse - this has never had any issues with needing namespaces (we used to get the xml from a different system).

The second is sent to an external company to show information on their site

The schema for the external site has a targetNamespace set in the route node:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.webaddress.co.uk/sch/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="area">
        <xs:element name="name" type="xs:string" />
        <xs:element name="url" type="xs:string" />
        <xs:element minOccurs="0" name="description" type="xs:string" />
        <xs:element name="engagement-methods">
              <xs:element maxOccurs="unbounded" name="method">
                    <xs:element name="title" type="xs:string" />
                    <xs:element name="description" type="xs:string" />
                    <xs:element minOccurs="0" name="url" type="xs:string" />

In that case it appears you should assign the namespace to the element which contains the attribute(s).