Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to create an XML document . With in there is a element called Column with property xsi:type="SQLINT". But eventhough i am describing the attribute its only creating type.Code i applied is below

C#
attribute = xmlDoc.CreateAttribute("xsi:type");
attribute.Value = "CharTerm";
userNode.Attributes.Append(attribute);
Posted

1 solution

You need to specify the namespace when you create the attribute:

C#
attribute = doc.CreateAttribute("xsi:type", "http://www.w3.org/2001/XMLSchema-instance");


However this will add the xmlns declaration to the child node:
XML
<doc>
  <user xsi:type="CharTerm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</doc>


If you want it to be on the root element you need to declare it explicitly:
C#
string xsi = "http://www.w3.org/2001/XMLSchema-instance";
var xmlns = doc.CreateAttribute("xmlns:xsi", "http://www.w3.org/2000/xmlns/");
xmlns.Value = xsi;
doc.DocumentElement.Attributes.Append(xmlns);

attribute = doc.CreateAttribute("xsi:type", xsi);


Then you get this:
XML
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <user xsi:type="CharTerm" />
</doc>
 
Share this answer
 
Comments
Tomas Takac 3-Nov-14 3:24am    
I don't understand. Is it ok now or you still have problems?
Arjun Menon U.K 6-Nov-14 7:29am    
I still do have problem
Tomas Takac 6-Nov-14 8:06am    
What is the problem? Please update the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900