C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

XML parser


Posted Date: 29 Dec 2008    Resource Type: Code Snippets    Category: XML
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 12



This code is for parsing any XML file.(i.e . reading its nodes)


public class mainparser
{
public static int countele=0;
public static void parsexml(string filename)
{
int starttick;
int endtick;
int i;
XmlTextReader rdr;
StringBuilder sb;
SimpleElement se = new SimpleElement("junk");
SimpleDOMParser sdp;
sb = new StringBuilder();
try
{
starttick = System.Environment.TickCount;
for (i = 0; i < 10; i++)
{
// OpenFileDialog ope = new OpenFileDialog();
// if (ope.ShowDialog() == DialogResult.OK)
//{

rdr = new XmlTextReader(filename);
sdp = new SimpleDOMParser();
se = sdp.parse(rdr);
rdr.Close();
//}
//else
//{
// MessageBox.Show("Sorry File cant be open.");
//}
}
endtick = System.Environment.TickCount;
sb = new StringBuilder();
printTree(se, sb, 0);
MessageBox.Show(sb.ToString());
TextWriter tw = new StreamWriter(filename+".csv");
tw.Write(sb.ToString());
tw.Close();
System.Diagnostics.Debug.WriteLine(sb.ToString());
System.Diagnostics.Debug.WriteLine(endtick - starttick);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private static void printTree(SimpleElement se, StringBuilder sb, int depth)
{
sb.Append(se.TagName+",");
//foreach (string attName in se.Attributes.Keys)
//{
// sb.Append(" " + attName + "=" + "\"" + se.Attribute(attName) + "\"");
//}
//if (countele == 0)
//{
//}
//else
//{
sb.Append(se.Text.Trim() + ",");
//}
if (se.ChildElements.Count > 0)
{
countele += 1;
//sb.Append(System.Environment.NewLine);
depth += 1;
foreach (SimpleElement ch in se.ChildElements)
{
printTree(ch, sb, depth);
}
depth -= 1;
}
else
{
//sb.Append(System.Environment.NewLine);
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Parser  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Create new Element ,Text,Attributes into a file
Previous Resource: SAX parser
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use