public static void Compress(XElement xml)
{
var typeLookup = new Dictionary<string, int>( ) ;
int found = 0 ;
xml.Descendants( ).Attributes( ).Where( a => a.Name.LocalName.StartsWith( @"ClrType" ) ).ForEach(
attribute =>
{
string typeName = attribute.Value ;
if( !typeLookup.ContainsKey( typeName ) )
{
typeLookup[ typeName ] = found++ ;
}
attribute.Value = typeLookup[ typeName ].ToString( ) ;
} ) ;
xml.Descendants( ).Where( a => a.Name.LocalName.StartsWith( @"ClrType" ) ).ForEach(
element =>
{
string typeName = element.Value ;
if( !typeLookup.ContainsKey( typeName ) )
{
typeLookup[ typeName ] = found++ ;
}
element.Value = typeLookup[ typeName ].ToString( ) ;
} ) ;
var typeMapElement = new XElement(
@"TypeMap",
typeLookup.Select(
p =>
new XElement(
@"Type",
new XAttribute( "Name", p.Key ),
new XAttribute( @"Index", p.Value ) ) ) ) ;
xml.Add( typeMapElement ) ;
}