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 »

Read XML


Posted Date: 18 Nov 2008    Resource Type: Code Snippets    Category: XML
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 7



This method will read a XML and will return output as
Count number of Comments, Attributes, Whitespaces etc. in an XML
Here I have taken a sample XML named sam.xml


private void ledger_Load(object sender, EventArgs e)
{
int ws = 0;
int pi = 0;
int dc = 0;
int cc = 0;
int ac = 0;
int et = 0;
int el = 0;
int xd = 0;

// Read a document

XmlTextReader textReader = new XmlTextReader("sam.xml");

// Read until end of file

while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
// If node type us a declaration
if (nType == XmlNodeType.XmlDeclaration)
{
MessageBox.Show("Declaration:" + textReader.Name.ToString());
xd = xd + 1;
}
// if node type is a comment
if (nType == XmlNodeType.Comment)
{
MessageBox.Show("Comment:" + textReader.Name.ToString());
cc = cc + 1;
}
// if node type us an attribute
if (nType == XmlNodeType.Attribute)
{
MessageBox.Show("Attribute:" + textReader.Name.ToString());
ac = ac + 1;
}
// if node type is an element
if (nType == XmlNodeType.Element)
{
MessageBox.Show("Element:" + textReader.Name.ToString());
el = el + 1;
}
// if node type is an entity\
if (nType == XmlNodeType.Entity)
{
MessageBox.Show("Entity:" + textReader.Name.ToString());
et = et + 1;
}
// if node type is a Process Instruction
if (nType == XmlNodeType.Entity)
{
MessageBox.Show("Entity:" + textReader.Name.ToString());
pi = pi + 1;
}
// if node type a document
if (nType == XmlNodeType.DocumentType)
{
MessageBox.Show("Document:" + textReader.Name.ToString());
dc = dc + 1;
}
// if node type is white space
if (nType == XmlNodeType.Whitespace)
{
MessageBox.Show("WhiteSpace:" + textReader.Name.ToString());
ws = ws + 1;
}
}
// Write the summary
MessageBox.Show("Total Comments:" + cc.ToString());
MessageBox.Show("Total Attributes:" + ac.ToString());
MessageBox.Show("Total Elements:" + el.ToString());
MessageBox.Show("Total Entity:" + et.ToString());
MessageBox.Show("Total Process Instructions:" + pi.ToString());
MessageBox.Show("Total Declaration:" + xd.ToString());
MessageBox.Show("Total DocumentType:" + dc.ToString());
MessageBox.Show("Total WhiteSpaces:" + ws.ToString());
}


Also I am attaching my sample XML if you want to have an idea

Attachments

  • Read XML (22541-18518-sam.xml)


  • 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.
    Read  .  

    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: Importing XML Into a Recordset
    Previous Resource: Recursive TeeNode Comparision
    Return to Discussion Resource Index
    Post New Resource
    Category: XML


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use