TechShri from Shriniwas Wani

Custom Search

27 June, 2006

Read Xml File - Approach 2

http://samples.gotdotnet.com/quickstart/howto/doc/Xml/WriteXMLFile.aspx

public static void ReadXMLApproach1()
{
StringReader stream;
XmlTextReader reader = null;
stream = new StringReader("" +
"" +
"" +
" " +
" " +
" " +
" Benjamin" +
" Franklin" +
"
" +
" 8.99" +
"
" +
" " +
" " +
" " +
" Herman" +
" Melville" +
"
" +
" 11.99" +
"
" +
" " +
" " +
" " +
" Plato" +
"
" +
" 9.99" +
"
" +
"
");
reader = new XmlTextReader(stream);
Console.WriteLine("Processing ...");
Console.WriteLine();
LoopThroughXMLReader(reader);
reader.Close();
}



private static void LoopThroughXMLReader(XmlReader reader)
{
int piCount = 0, docCount = 0, commentCount = 0, elementCount = 0, attributeCount = 0, textCount = 0, whitespaceCount = 0;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.ProcessingInstruction:
Format(reader, "ProcessingInstruction");
piCount++;
break;


case XmlNodeType.DocumentType:
Format(reader, "DocumentType");
docCount++;
break;

case XmlNodeType.Comment:
Format(reader, "Comment");
commentCount++;
break;

case XmlNodeType.Element:
Format(reader, "Element");
while (reader.MoveToNextAttribute())
{
Format(reader, "Attribute");
}
elementCount++;
if (reader.HasAttributes)
attributeCount += reader.AttributeCount;
break;

case XmlNodeType.Text:
Format(reader, "Text");
textCount++;
break;

case XmlNodeType.Whitespace:
whitespaceCount++;
break;

}
}
}


// Format the output
private static void Format(XmlReader reader, String nodeType)
{
// Format the output
Console.Write(reader.Depth + " ");
Console.Write(reader.AttributeCount + " ");
for (int i = 0; i <>" + reader.Value);
Console.WriteLine();
}

0 Comments:

Post a Comment

<< Home