' XmlStatistic by Martin Szugat Imports System.Xml Imports System.Xml.Serialization Module Main Sub Main() Dim xtr As XmlTextReader Dim xtw As XmlTextWriter Dim xst As New XmlStatistics Try Dim args As String() = Environment.GetCommandLineArgs If args.Length > 1 Then For i As Integer = 1 To args.Length - 1 xst.documents += 1 xtr = New XmlTextReader(args(i)) analyze(xtr, xst) xtr.Close() xtr = Nothing Next Else xst.documents = 1 xtr = New XmlTextReader(Console.OpenStandardInput) analyze(xtr, xst) End If If xst.dataChars > 0 Then xst.metaDataRatio = xst.metaChars / xst.dataChars xst.whiteSpaceDataRatio = xst.whiteSpaceChars / xst.dataChars xst.nonDataDataRatio = (xst.metaChars + xst.whiteSpaceChars) / xst.dataChars End If xtw = New XmlTextWriter(Console.OpenStandardOutput, Nothing) xtw.Formatting = Formatting.Indented Dim xs As New XmlSerializer(GetType(XmlStatistics)) xs.Serialize(xtw, xst) Catch ex As Exception Console.Error.WriteLine(ex) Finally If Not xtr Is Nothing Then xtr.Close() If Not xtw Is Nothing Then xtw.Close() End Try End Sub Private Sub analyze(ByVal xr As XmlReader, ByVal xst As XmlStatistics) While xr.Read Select Case xr.NodeType Case XmlNodeType.CDATA xst.dataChars += xr.Value.Length Case XmlNodeType.Comment xst.comments += 1 Case XmlNodeType.Element xst.elements += 1 xst.metaChars += xr.Name.Length + 2 ' chars: <, > If xr.IsEmptyElement Then xst.metaChars += 1 ' char: / End If If xr.MoveToFirstAttribute Then Do xst.attributes += 1 xst.metaChars += xr.Name.Length + 4 ' chars: Space, =, ", " xst.dataChars += xr.Value.Length Loop While xr.MoveToNextAttribute() xr.MoveToElement() End If Case XmlNodeType.EndElement xst.metaChars += xr.Name.Length + 3 ' chars: <, /, > Case XmlNodeType.ProcessingInstruction xst.processingInstructions += 1 Case XmlNodeType.SignificantWhitespace xst.dataChars += xr.Value.Length Case XmlNodeType.Text xst.dataChars += xr.Value.Length Case XmlNodeType.Whitespace xst.whiteSpaceChars += xr.Value.Length Case Else ' Ignore End Select End While End Sub End Module Public Class XmlStatistics Public documents As Integer = 0 Public elements As Integer = 0 Public attributes As Integer = 0 Public processingInstructions As Integer = 0 Public comments As Integer = 0 Public whiteSpaceChars As Integer = 0 Public metaChars As Integer = 0 Public dataChars As Integer = 0 Public metaDataRatio As Double = 0 Public whiteSpaceDataRatio As Double = 0 Public nonDataDataRatio As Double = 0 End Class