User defined 3" /> User defined 3" /> User defined 3"/>

C# XMLWriter create WriteAttributeStringAsync

143 views Asked by At

Does anyone know how I can write this line with the XMLWriter?

<TextChild xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string">User defined 3</TextChild>

Problem here is xmlns:dt and dt:dt.

Error Message: 'dt' is a duplicate attribute name.

await writer.WriteStartElementAsync(string.Empty, "TextChild", string.Empty);
await writer.WriteAttributeStringAsync("xmlns", "dt", string.Empty, "urn:schemas-microsoft-com:datatypes");
await writer.WriteAttributeStringAsync("dt", "dt", string.Empty, "boolean");
await writer.WriteStringAsync(name);
await writer.WriteEndElementAsync();//TextChild
1

There are 1 answers

0
Alexander Petrov On

xmlns:dt is a namespace definition. This is not an attribute.
It does not need to be created manually. It is enough to specify the namespace when creating the attribute.

await writer.WriteStartElementAsync(null, "TextChild", null);
await writer.WriteAttributeStringAsync("dt", "dt", "urn:schemas-microsoft-com:datatypes", "string");
await writer.WriteStringAsync(name);
await writer.WriteEndElementAsync();