Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Friends,

can any budy tell me that how to create xml file as given example below :

<etaalup_count>
<countnodes serviceid="A022114704152" count="10" />
<countnodes serviceid="A022114704153" count="15" />
<countnodes serviceid="A022114704154" count="5" />
</etaalup_count>

Thanks & Regards
Hussain
Posted
Updated 17-Jul-13 22:15pm
v2

Just using Google, you may find many tutorials on the web.
For instance, try reading this one: C# XmlWriter[^].
 
Share this answer
 
Comments
Pheonyx 18-Jul-13 5:05am    
My +5 people should have Googled before posting here for such basic bits like this!
CPallini 19-Jul-13 3:19am    
Thank you very much.
A very basic form that get you going and you can improve the code as you want.


C#
static void CreateXML()
{
    XmlDocument doc = new XmlDocument();

    XmlNode rootNode = doc.CreateElement("etaalup_count");
    doc.AppendChild(rootNode);

    XmlNode countNode = doc.CreateElement("countnodes");
    XmlAttribute serviceAttribute = doc.CreateAttribute("serviceid");
    serviceAttribute.Value = "A022114704152";
    countNode.Attributes.Append(serviceAttribute);
    XmlAttribute countAttribute = doc.CreateAttribute("count");
    countAttribute.Value = "10";
    countNode.Attributes.Append(countAttribute);
    rootNode.AppendChild(countNode);

    countNode = doc.CreateElement("countnodes");
    serviceAttribute = doc.CreateAttribute("serviceid");
    serviceAttribute.Value = "A022114704153";
    countNode.Attributes.Append(serviceAttribute);
    countAttribute = doc.CreateAttribute("count");
    countAttribute.Value = "15";
    countNode.Attributes.Append(countAttribute);
    rootNode.AppendChild(countNode);

    countNode = doc.CreateElement("countnodes");
    serviceAttribute = doc.CreateAttribute("serviceid");
    serviceAttribute.Value = "A022114704154";
    countNode.Attributes.Append(serviceAttribute);
    countAttribute = doc.CreateAttribute("count");
    countAttribute.Value = "5";
    countNode.Attributes.Append(countAttribute);

    rootNode.AppendChild(countNode);
    doc.Save(Console.Out);
}
 
Share this answer
 
The WriteChars method of the XmlWriter class writes characters to XML. It takes an array of characters and writes one character at a time.

The following code snippet takes an array of characters and writes them to an XML file.

using (XmlWriter writer = XmlWriter.Create("M.xml")) {
writer.WriteStartDocument();

char[] ch = new char[6];
ch[0] = 'm';
ch[1] = 'a';
ch[2] = 'h';
ch[3] = 'e';
ch[4] = 's';
ch[5] = 'h';

writer.WriteStartElement("WriteChars");

writer.WriteChars(ch, 0, ch.Length);

writer.WriteEndElement();

writer.WriteEndDocument();

}
 
Share this answer
 
You can create xml from database table like this:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class myxml : System.Web.UI.Page
{

protected void Button1_Click(object sender, EventArgs e)
{
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from demo", "Data Source=184.107.55.15;Initial Catalog=training;User ID=ing;Password=ing");
da.Fill(ds);
// Write to XML here

ds.WriteXml("D:\\output.xml", System.Data.XmlWriteMode.WriteSchema);
}
}
 
Share this answer
 

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