Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

i want to create xml line like:

XML
<field value="95010" name="ORDERNUMBER" />


how can i create this?

can you help?

What I have tried:

XmlElement _f1 = _data.CreateElement("");
_f1.SetAttribute("field value", "95010");
_f1.SetAttribute("name","ORDERNUMBER");

but its not working
Posted
Updated 28-Mar-16 22:23pm
v2

Try this

C#
XmlDocument xmlDoc = new XmlDocument();
           XmlElement rootElem = xmlDoc.CreateElement("xml");
           XmlElement field = xmlDoc.CreateElement("field");
           field.SetAttribute("value", "95010");
           field.SetAttribute("name", "ORDERNUMBER");
           rootElem.AppendChild(field);
           xmlDoc.AppendChild(rootElem);
           xmlDoc.Save("output.xml");


it results

XML
<field value="95010" name="ORDERNUMBER" />
 
Share this answer
 
Comments
Member 10525430 29-Mar-16 4:46am    
its comming like:
<pre lang="xml"><field name="ORDERNUMBER" value="95010">;
its cross i cant understand why it it
Karthik_Mahalingam 29-Mar-16 4:56am    
that is syntax formatter in codeproject..
your post resolved or stil you have some issue.
phil.o 29-Mar-16 5:03am    
Because the XML formatter you are using is sorting attributes alphabetically. In what is this a problem?
 
Share this answer
 
Comments
Member 10525430 29-Mar-16 3:42am    
are you kidding me?
what i asked? and what did you answer?
look at my question.
i know how can i create xml
but i cant create which have 2 attributes like that.

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