Windows Phone Developers

Wednesday, September 17, 2008

Create XML Files using .NET / Create XML Files using C#

Writing XML Files using C#

C# has many methods to create XML files with ease. XMLTextWriter class is an useful one containing methods to create a formatted and valid XML.

It represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations

private void write2XML()

{

// Sample Invoice for Ganesh Padmavathy GmBH.

XmlTextWriter Writer = new XmlTextWriter(@"c:\ShasurData\ForBlogger\Sample.xml",Encoding.ASCII );

// Write XML with Indent

Writer.Formatting = Formatting.Indented;

// Write XML Declaration

Writer.WriteStartDocument(true);

// Writing Comments in XML File

Writer.WriteComment("Sample Invoice for GP GmbH");

// Root Element

Writer.WriteStartElement("Invoice");

Writer.WriteAttributeString("INVNo", "INT03456");

Writer.WriteAttributeString("InvDate", DateTime.Now.ToString() );

Writer.WriteStartElement ("Items");

Writer.WriteStartElement("Item");

//Writer.WriteString("Sample String");

Writer.WriteElementString("Name", "Hex Socket Screw");

Writer.WriteElementString("Price", "$0.25");

Writer.WriteElementString("Quantity", "1000");

Writer.WriteEndElement(); // Item End Tag

Writer.WriteStartElement("Item");

Writer.WriteElementString("Name", "Roll Bearing");

Writer.WriteElementString("Price", "$0.15");

Writer.WriteElementString("Quantity", "1200");

Writer.WriteEndElement(); // Item End Tag

Writer.WriteEndElement(); // Items End Tag

Writer.WriteEndElement(); // Invoice End Tag

Writer.WriteEndDocument();

Writer.Flush();

Writer.Close();

}

Write NameSpace of XML Specification in C#

Writer.WriteStartDocument(true) is used to write the XML declaration with the version "1.0".

Create Indented XML Files using C#

XmlTextWriter..::.Formatting Property is used to create XML Documents with proper indentation .

Writer.Formatting = Formatting.Indented;

Will give a indented XML as shown below

< ?xml version="1.0" encoding="us-ascii" standalone="yes"? >

< !--Sample Invoice for GP GmbH-- >

< Invoice INVNo="INT03456" d1p1:vbadud="15-09-2008 08:41:42" xmlns:d1p1="InvDate" >

< Items >

< Item >

< Name >Hex Socket Screw< /Name >

< Price >$0.25< /Price >

< Quantity >1000< /Quantity >

< /Item >

< Item >

< Name >Roll Bearing< /Name >

< Price >$0.15< /Price >

< Quantity >1200< /Quantity >

< /Item >

< /Items >

< /Invoice >

Indented XML Files using C#, Formatting XML Files using C#, Create a Formatted XML file using C#, Writing XML Files using C#, C# Create XML Files, How to write XML Specification using C#

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Create XML Files using .NET / Create XML Files using C#

Writing XML Files using C#

C# has many methods to create XML files with ease. XMLTextWriter class is an useful one containing methods to create a formatted and valid XML.

It represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations

private void write2XML()

{

// Sample Invoice for Ganesh Padmavathy GmBH.

XmlTextWriter Writer = new XmlTextWriter(@"c:\ShasurData\ForBlogger\Sample.xml",Encoding.ASCII );

// Write XML with Indent

Writer.Formatting = Formatting.Indented;

// Write XML Declaration

Writer.WriteStartDocument(true);

// Writing Comments in XML File

Writer.WriteComment("Sample Invoice for GP GmbH");

// Root Element

Writer.WriteStartElement("Invoice");

Writer.WriteAttributeString("INVNo", "INT03456");

Writer.WriteAttributeString("InvDate", DateTime.Now.ToString() );

Writer.WriteStartElement ("Items");

Writer.WriteStartElement("Item");

//Writer.WriteString("Sample String");

Writer.WriteElementString("Name", "Hex Socket Screw");

Writer.WriteElementString("Price", "$0.25");

Writer.WriteElementString("Quantity", "1000");

Writer.WriteEndElement(); // Item End Tag

Writer.WriteStartElement("Item");

Writer.WriteElementString("Name", "Roll Bearing");

Writer.WriteElementString("Price", "$0.15");

Writer.WriteElementString("Quantity", "1200");

Writer.WriteEndElement(); // Item End Tag

Writer.WriteEndElement(); // Items End Tag

Writer.WriteEndElement(); // Invoice End Tag

Writer.WriteEndDocument();

Writer.Flush();

Writer.Close();

}

Write NameSpace of XML Specification in C#

Writer.WriteStartDocument(true) is used to write the XML declaration with the version "1.0".

Create Indented XML Files using C#

XmlTextWriter..::.Formatting Property is used to create XML Documents with proper indentation .

Writer.Formatting = Formatting.Indented;

Will give a indented XML as shown below

< ?xml version="1.0" encoding="us-ascii" standalone="yes"? >

< !--Sample Invoice for GP GmbH-- >

< Invoice INVNo="INT03456" d1p1:vbadud="15-09-2008 08:41:42" xmlns:d1p1="InvDate" >

< Items >

< Item >

< Name >Hex Socket Screw< /Name >

< Price >$0.25< /Price >

< Quantity >1000< /Quantity >

< /Item >

< Item >

< Name >Roll Bearing< /Name >

< Price >$0.15< /Price >

< Quantity >1200< /Quantity >

< /Item >

< /Items >

< /Invoice >

Indented XML Files using C#, Formatting XML Files using C#, Create a Formatted XML file using C#, Writing XML Files using C#, C# Create XML Files, How to write XML Specification using C#

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Write Text Files using C#

C# Write Text Files / Write ASCII Files using C#

private void write_to_textfile()

{

try

{

StreamWriter SW = new StreamWriter(@"c:\vbadud\Sample.txt");

SW.Write ("This is a sample text");

SW.Flush() ;

SW.Close();

}

catch(Exception ex)

{

MessageBox.Show("Error writing file := " + ex.Message );

}

}

See Also:

Combining Text Files using VBA

Spacing in Text Files

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Saturday, September 6, 2008

Unrecognized escape sequence (error CS1009)

A common case for this error is using the backslash character in a file name. Replace “\” with “\\” or use “@” prefix for the literal as shown below




Error



StreamWriter SW = new StreamWriter("c:\shasurdata\forblogger\Sample.txt");


Corrected



StreamWriter SW = new StreamWriter("c:\\shasurdata\\forblogger\\Sample.txt");



Or



StreamWriter SW = new StreamWriter(@"c:\shasurdata\forblogger\Sample.txt");



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

System.IndexOutOfRangeException was unhandled (Message="Index was outside the bounds of the array.")

The exception that is thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array. This class cannot be
inherited.

.NET arrays have base 0 and should be kept in mind when coding.

Index was outside the bounds of the array Error
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Wednesday, September 3, 2008

Creating and Sorting Arrays in C#

private void csharp_array()

{

string[] arCities = {"","","",""};

arCities[0] = "Zurich";

arCities[1] = "Havana";

arCities[2] = "Amalapuram";

arCities[3] = "San Francisco";

//arnames

Array.Sort(arCities);

}

Array.Sort method sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.



Before Sorting

After Sorting


Sorting Arrays in C#, Creating Arrays in C#, Array.Sort in C#, Sorting One Dimensional arrays using C#, C# Array sort method Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon