CSharp and XML Generation

  • Thread starter Thread starter Ritu
  • Start date Start date
R

Ritu

Hi ,

I am new to C#. Can any please tell me how i can generate XML using C#. I
need to pass some configuration settings to some other application.

Regards
 
There are couple of ways to create an XML document.

If you want to create an XML representation of a class you should
consider using Xml serialization. Check the following :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml01202003.asp
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=236

Now, if you want to create an XML document without using a class you
should check the System.Xml namespace. A small example could be
(coppied from MSDN)

static void WriteQuote(XmlWriter writer, string symbol,
double price, double change, long volume)
{
writer.WriteStartElement("Stock");
writer.WriteAttributeString("Symbol", symbol);
writer.WriteElementString("Price", XmlConvert.ToString(price));
writer.WriteElementString("Change", XmlConvert.ToString(change));
writer.WriteElementString("Volume", XmlConvert.ToString(volume));
writer.WriteEndElement();
}

public static void Main(){
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
WriteQuote(writer, "MSFT", 74.125, 5.89, 69020000);
writer.Close();
}

Check this also :
ms-help://MS.MSDNQTR.2005APR.1033/cpguide/html/cpconWritingXMLWithXmlWriter.htm
 
Ritu,

Try http://www.codeproject.com/useritems/XmlHelper.asp for a tutorial
and both C# and VB.Net source code for a handy XmlHelper class
probably does everything you're looking to do.

Hope this helps...
-- Steve

Hi ,

I am new to C#. Can any please tell me how i can generate XML using C#. I
need to pass some configuration settings to some other application.

Regards

Thanks,

Steve Shaffar
The Shaffar Group, Inc.

Master Software Developer
and Applications Designer
 
John makes a good point regarding XmlWriter.

The XmlHelper Demo at codeproject is a class with methods to create
and manipulate an XML Document. The methods are easier to work with
than Xpath queries and Navigator -- especially for someone who is new
to C# and XML. Rudy Guzman developed this class so that all you have
to do is create an instance and call the appropriate methods. The
source can be downloaded and studied to see how it works.

Regards,
Steve
 
Back
Top