Last active
December 17, 2015 01:09
-
-
Save serge1/5525982 to your computer and use it in GitHub Desktop.
Print XML file structure for all XML files in given directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.IO; | |
using System.Xml.Linq; | |
namespace xml_struct | |
{ | |
//----------------------------------------------------------------------- | |
class Program | |
{ | |
//------------------------------------------------------------------- | |
static void | |
Main( string[] args ) | |
{ | |
if ( args.Length != 1 && args.Length != 2 ) { | |
Console.WriteLine( | |
"Usage: xml_struct <directory> [<output_prefix>]" ); | |
return; | |
} | |
XmlStructureReader reader = new XmlStructureReader(); | |
try { | |
foreach ( var file in | |
Directory.EnumerateFiles( args[0], "*.xml" ) ) { | |
reader.CollectXmlStructure( file ); | |
} | |
string prefix = ""; | |
if ( args.Length == 2 ) { | |
prefix = args[1]; | |
} | |
reader.PrintCollectedTree( prefix ); | |
} | |
catch ( Exception e ) { | |
Console.WriteLine( "{0}", e.Message ); | |
} | |
} | |
} | |
//----------------------------------------------------------------------- | |
class XmlStructureReader | |
{ | |
SortedDictionary<string, SortedSet<string>> elements; | |
//------------------------------------------------------------------- | |
public | |
XmlStructureReader() | |
{ | |
elements = new SortedDictionary<string, SortedSet<string>>(); | |
} | |
//------------------------------------------------------------------- | |
public void | |
CollectXmlStructure( string filename ) | |
{ | |
try { | |
XElement root = XElement.Load( filename ); | |
CollectElements( root, "" ); | |
} | |
catch { | |
} | |
} | |
//------------------------------------------------------------------- | |
public void | |
PrintCollectedTree( string prefix ) | |
{ | |
foreach ( var element in elements ) { | |
if ( !element.Key.StartsWith( prefix ) ) { | |
continue; | |
} | |
Console.WriteLine( "{0}", element.Key ); | |
foreach ( var attribute in element.Value ) { | |
Console.WriteLine( " {0}", attribute ); | |
} | |
} | |
} | |
//------------------------------------------------------------------- | |
private void | |
CollectElements( XElement node, string current_path ) | |
{ | |
current_path += "/" + node.Name; | |
if ( !elements.ContainsKey( current_path ) ) { | |
elements.Add( current_path, new SortedSet<string>() ); | |
} | |
foreach ( XAttribute attribute in node.Attributes() ) { | |
elements[current_path].Add( attribute.Name.LocalName ); | |
} | |
var children = node.Elements(); | |
foreach ( XElement child in children ) { | |
CollectElements( child, current_path ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment