File: SchemaMerger.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (771 lines) | stat: -rw-r--r-- 30,913 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#region Copyright (c) Microsoft Corporation
/// <copyright company='Microsoft Corporation'>
///    Copyright (c) Microsoft Corporation. All Rights Reserved.
///    Information Contained Herein is Proprietary and Confidential.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;

#if WEB_EXTENSIONS_CODE
using System.Web.Resources;
#else
using Microsoft.VSDesigner.WCF.Resources;
#endif

#if WEB_EXTENSIONS_CODE
namespace System.Web.Compilation.WCFModel
#else
namespace Microsoft.VSDesigner.WCFModel
#endif
{
    /// <summary>
    /// a utility class to merge schema files, and remove duplicated part
    /// </summary>
    internal class SchemaMerger
    {
        // xml serializable attributes
        private static Type[] xmlSerializationAttributes = new Type[] {
                            typeof(System.Xml.Serialization.XmlElementAttribute),
                            typeof(System.Xml.Serialization.XmlAttributeAttribute),
                            typeof(System.Xml.Serialization.XmlAnyAttributeAttribute),
                            typeof(System.Xml.Serialization.XmlAnyElementAttribute),
                            typeof(System.Xml.Serialization.XmlTextAttribute),
        };

        // elements in the schema (we don't process annotation node)
        private static SchemaTopLevelItemType[] schemaTopLevelItemTypes = new SchemaTopLevelItemType[] {
            new SchemaTopLevelItemType(typeof(XmlSchemaType), "type"),
            new SchemaTopLevelItemType(typeof(XmlSchemaElement), "element"),
            new SchemaTopLevelItemType(typeof(XmlSchemaAttribute), "attribute"),
            new SchemaTopLevelItemType(typeof(XmlSchemaGroup), "group"),
            new SchemaTopLevelItemType(typeof(XmlSchemaAttributeGroup), "attributeGroup"),
        };

        // when properties defined in those types are different, we only report warnings, but not error messages
        private static Type[] ignorablePropertyTypes = new Type[] {
            typeof(XmlAttribute[]),
            typeof(XmlElement[]),
            typeof(XmlNode[]),
            typeof(XmlSchemaAnnotation),
        };

        private readonly static XmlAttribute[] emptyXmlAttributeCollection = new XmlAttribute[0];
        private readonly static object[] emptyCollection = new object[0];

        /// <summary>
        /// Merge and remove duplicated part from the schema list
        /// </summary>
        /// <param name="schemaList">schemas with names</param>
        /// <param name="importErrors">error messages</param>
        /// <param name="duplicatedSchemas">error messages</param>
        /// <remarks></remarks>
        internal static void MergeSchemas(IEnumerable<XmlSchema> schemaList, IList<ProxyGenerationError> importErrors, out IEnumerable<XmlSchema> duplicatedSchemas)
        {
            if (schemaList == null)
            {
                throw new ArgumentNullException("schemaList");
            }
            if (importErrors == null)
            {
                throw new ArgumentNullException("importErrors");
            }

            List<XmlSchema> duplicatedSchemaList = new List<XmlSchema>();
            duplicatedSchemas = duplicatedSchemaList;

            // types, elements, groups have their own name space
            Dictionary<XmlQualifiedName, XmlSchemaObject>[] knownItemTables = new Dictionary<XmlQualifiedName, XmlSchemaObject>[schemaTopLevelItemTypes.Length];
            for (int i = 0; i < schemaTopLevelItemTypes.Length; i++)
            {
                knownItemTables[i] = new Dictionary<XmlQualifiedName, XmlSchemaObject>();
            }

            foreach (XmlSchema schema in schemaList)
            {

                bool hasNewDefinedItems = false;
                List<XmlSchemaObject> duplicatedItems = new List<XmlSchemaObject>();

                for (int i = 0; i < schemaTopLevelItemTypes.Length; i++)
                {
                    Dictionary<XmlQualifiedName, XmlSchemaObject> knownItemTable = knownItemTables[i];
                    int knownItemCount = knownItemTable.Count;
                    FindDuplicatedItems(schema, schemaTopLevelItemTypes[i].ItemType, schemaTopLevelItemTypes[i].Name, knownItemTable, duplicatedItems, importErrors);
                    if (knownItemTable.Count > knownItemCount)
                    {
                        hasNewDefinedItems = true;
                    }
                }

                if (duplicatedItems.Count > 0)
                {
                    if (!hasNewDefinedItems)
                    {
                        // remove the whole schema...
                        duplicatedSchemaList.Add(schema);
                    }
                    else
                    {
                        // remove duplicated items only
                        foreach (XmlSchemaObject item in duplicatedItems)
                        {
                            schema.Items.Remove(item);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Find duplicated items in a schema
        /// </summary>
        /// <remarks></remarks>
        private static void FindDuplicatedItems(
                    XmlSchema schema,
                    Type itemType,
                    string itemTypeName,
                    Dictionary<XmlQualifiedName, XmlSchemaObject> knownItemTable,
                    List<XmlSchemaObject> duplicatedItems,
                    IList<ProxyGenerationError> importErrors)
        {

            string targetNamespace = schema.TargetNamespace;
            if (String.IsNullOrEmpty(targetNamespace))
            {
                targetNamespace = String.Empty;
            }

            foreach (XmlSchemaObject item in schema.Items)
            {
                if (itemType.IsInstanceOfType(item))
                {

                    XmlQualifiedName combinedName = new XmlQualifiedName(GetSchemaItemName(item), targetNamespace);

                    XmlSchemaObject originalItem = null;
                    if (knownItemTable.TryGetValue(combinedName, out originalItem))
                    {
                        string differentLocation;

                        if (!AreSchemaObjectsEquivalent(originalItem, item, out differentLocation))
                        {
                            differentLocation = CombinePath(".", differentLocation);
                            importErrors.Add(
                                    new ProxyGenerationError(
                                        ProxyGenerationError.GeneratorState.MergeMetadata,
                                        String.Empty,
                                        new InvalidOperationException(
                                            String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_DuplicatedSchemaItems, itemTypeName, combinedName.ToString(), schema.SourceUri, originalItem.SourceUri, differentLocation)
                                        )
                                    )
                            );
                        }
                        else if (!String.IsNullOrEmpty(differentLocation))
                        {
                            // warning: ignorable difference found
                            differentLocation = CombinePath(".", differentLocation);
                            importErrors.Add(
                                    new ProxyGenerationError(
                                        ProxyGenerationError.GeneratorState.MergeMetadata,
                                        String.Empty,
                                        new InvalidOperationException(
                                            String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_DuplicatedSchemaItemsIgnored, itemTypeName, combinedName.ToString(), schema.SourceUri, originalItem.SourceUri, differentLocation)
                                        ),
                                        true        // isWarning = true
                                    )
                            );
                        }
                        duplicatedItems.Add(item);
                    }
                    else
                    {
                        item.SourceUri = schema.SourceUri;
                        knownItemTable.Add(combinedName, item);
                    }
                }
            }
        }

        /// <summary>
        /// Compare two schema objects
        /// </summary>
        /// <return></return>
        /// <remarks>
        /// For all those functions, we follow the same pattern:
        ///  return false: find not-ignorable difference between them.  differentLocation will contain the path
        ///  return true, with empty differentLocation -- no difference found
        ///  return true, with non-empty differentLocation -- ignorable difference found under that location
        /// </remarks>
        private static bool AreSchemaObjectsEquivalent(XmlSchemaObject originalItem, XmlSchemaObject item, out string differentLocation)
        {
            differentLocation = String.Empty;

            Type itemType = originalItem.GetType();
            if (itemType != item.GetType())
            {
                return false;
            }

            string ignorableDifferenceLocation = String.Empty;

            PropertyInfo[] properties = itemType.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (IsPersistedProperty(property))
                {

                    bool ignorableProperty = ShouldIgnoreSchemaProperty(property);

                    object originalValue = property.GetValue(originalItem, new object[] { });
                    object newValue = property.GetValue(item, new object[] { });

                    if (!CompareSchemaPropertyValues(property, originalValue, newValue, out differentLocation) && !ignorableProperty)
                    {
                        return false;
                    }
                    if (String.IsNullOrEmpty(ignorableDifferenceLocation))
                    {
                        ignorableDifferenceLocation = differentLocation;
                    }
                }
            }

            differentLocation = ignorableDifferenceLocation;
            return true;
        }

        /// <summary>
        /// Compare two property of a schema object
        /// </summary>
        /// <return>true: if the value are same</return>
        /// <remarks></remarks>
        private static bool CompareSchemaPropertyValues(PropertyInfo propertyInfo, object originalValue, object newValue, out string differentLocation)
        {
            differentLocation = String.Empty;

            if (originalValue == null && newValue == null)
            {
                return true;
            }

            // we create empty collection so a meaningful differentLocation could be generated
            if (typeof(XmlAttribute[]) == propertyInfo.PropertyType)
            {
                if (originalValue == null)
                {
                    originalValue = emptyXmlAttributeCollection;
                }
                if (newValue == null)
                {
                    newValue = emptyXmlAttributeCollection;
                }

                XmlAttribute differentAttribute1, differentAttribute2;
                if (!CompareXmlAttributeCollections((XmlAttribute[])originalValue, (XmlAttribute[])newValue, out differentAttribute1, out differentAttribute2))
                {
                    differentLocation = GetSchemaPropertyNameInXml(propertyInfo, differentAttribute1, differentAttribute2);
                    return false;
                }
                return true;
            }

            if (typeof(System.Collections.ICollection).IsAssignableFrom(propertyInfo.PropertyType))
            {
                if (originalValue == null)
                {
                    originalValue = emptyCollection;
                }
                if (newValue == null)
                {
                    newValue = emptyCollection;
                }

                object differentItem1, differentItem2;
                if (!CompareSchemaCollections((System.Collections.ICollection)originalValue, (System.Collections.ICollection)newValue, out differentItem1, out differentItem2, out differentLocation))
                {
                    differentLocation = CombinePath(GetSchemaPropertyNameInXml(propertyInfo, differentItem1, differentItem2), differentLocation);
                    return false;
                }
                else
                {
                    if (!String.IsNullOrEmpty(differentLocation))
                    {
                        // ignorable difference...
                        differentLocation = CombinePath(GetSchemaPropertyNameInXml(propertyInfo, differentItem1, differentItem2), differentLocation);
                    }
                    return true;
                }
            }

            if (originalValue == null || newValue == null)
            {
                differentLocation = CombinePath(GetSchemaPropertyNameInXml(propertyInfo, originalValue, newValue), differentLocation);
                return false;
            }

            if (originalValue.GetType() != newValue.GetType())
            {
                differentLocation = CombinePath(GetSchemaPropertyNameInXml(propertyInfo, originalValue, newValue), differentLocation);
                return false;
            }

            if (!CompareSchemaValues(originalValue, newValue, out differentLocation))
            {
                differentLocation = CombinePath(GetSchemaPropertyNameInXml(propertyInfo, originalValue, newValue), differentLocation);
                return false;
            }
            else
            {
                // ignorable difference...
                if (!String.IsNullOrEmpty(differentLocation))
                {
                    differentLocation = CombinePath(GetSchemaPropertyNameInXml(propertyInfo, originalValue, newValue), differentLocation);
                }
                return true;
            }
        }

        /// <summary>
        /// Compare two schema values
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static bool CompareSchemaValues(object originalValue, object newValue, out string differentLocation)
        {
            differentLocation = String.Empty;

            if (originalValue == null || newValue == null)
            {
                return (originalValue == null && newValue == null);
            }

            if (originalValue.GetType() != newValue.GetType())
            {
                return false;
            }

            if (originalValue is XmlSchemaObject)
            {
                return AreSchemaObjectsEquivalent((XmlSchemaObject)originalValue, (XmlSchemaObject)newValue, out differentLocation);
            }

            if (originalValue is XmlAttribute)
            {
                return CompareXmlAttributes((XmlAttribute)originalValue, (XmlAttribute)newValue);
            }

            if (originalValue is XmlElement)
            {
                return CompareXmlElements((XmlElement)originalValue, (XmlElement)newValue, out differentLocation);
            }

            if (originalValue is XmlText)
            {
                return CompareXmlTexts((XmlText)originalValue, (XmlText)newValue);
            }

            return originalValue.Equals(newValue);
        }

        /// <summary>
        /// Compare two collections of items
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static bool CompareSchemaCollections(System.Collections.IEnumerable originalCollection, System.Collections.IEnumerable newCollection,
                out object differentItem1, out object differentItem2, out string differentLocation)
        {

            differentLocation = String.Empty;

            System.Collections.IEnumerator list1 = originalCollection.GetEnumerator();
            System.Collections.IEnumerator list2 = newCollection.GetEnumerator();

            string ignorableDifferenceLocation = String.Empty;
            object ignorableDifferenceItem1 = null;
            object ignorableDifferenceItem2 = null;

            do
            {
                differentItem1 = list1.MoveNext() ? list1.Current : null;
                differentItem2 = list2.MoveNext() ? list2.Current : null;

                if (!CompareSchemaValues(differentItem1, differentItem2, out differentLocation))
                {
                    return false;
                }

                if (String.IsNullOrEmpty(ignorableDifferenceLocation))
                {
                    ignorableDifferenceItem1 = differentItem1;
                    ignorableDifferenceItem2 = differentItem2;
                    ignorableDifferenceLocation = differentLocation;
                }
            }
            while (differentItem1 != null && differentItem2 != null);

            Debug.Assert(differentItem1 == null && differentItem2 == null);

            differentLocation = ignorableDifferenceLocation;
            differentItem1 = ignorableDifferenceItem1;
            differentItem2 = ignorableDifferenceItem2;

            return true;
        }

        /// <summary>
        /// Compare two attributes
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static bool CompareXmlAttributes(XmlAttribute attribute1, XmlAttribute attribute2)
        {
            return String.Equals(attribute1.LocalName, attribute2.LocalName, StringComparison.Ordinal) &&
                    String.Equals(attribute1.NamespaceURI, attribute2.NamespaceURI, StringComparison.Ordinal) &&
                    String.Equals(attribute1.Value, attribute2.Value, StringComparison.Ordinal);
        }

        /// <summary>
        /// Compare two attribute collections
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static bool CompareXmlAttributeCollections(System.Collections.ICollection attributeCollection1, System.Collections.ICollection attributeCollection2, out XmlAttribute differentAttribute1, out XmlAttribute differentAttribute2)
        {
            differentAttribute1 = null;
            differentAttribute2 = null;

            XmlAttribute[] attributeArray1 = GetSortedAttributeArray(attributeCollection1);
            XmlAttribute[] attributeArray2 = GetSortedAttributeArray(attributeCollection2);

            object differentItem1, differentItem2;
            string differentLocation;
            if (!CompareSchemaCollections(attributeArray1, attributeArray2, out differentItem1, out differentItem2, out differentLocation))
            {
                differentAttribute1 = (XmlAttribute)differentItem1;
                differentAttribute2 = (XmlAttribute)differentItem2;
                return false;
            }
            return true;
        }

        /// <summary>
        /// sort XmlAttribute array, so we can compare two collections without being affected by the order
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static XmlAttribute[] GetSortedAttributeArray(System.Collections.ICollection attributeCollection)
        {
            XmlAttribute[] attributeArray = new XmlAttribute[attributeCollection.Count];
            int index = 0;
            foreach (XmlAttribute attribute in attributeCollection)
            {
                attributeArray[index++] = attribute;
            }

            Array.Sort(attributeArray, new AttributeComparer());
            return attributeArray;
        }

        /// <summary>
        /// Compare two elements
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static bool CompareXmlElements(XmlElement element1, XmlElement element2, out string differentLocation)
        {
            differentLocation = String.Empty;

            if (!String.Equals(element1.LocalName, element2.LocalName, StringComparison.Ordinal) ||
                    !String.Equals(element1.NamespaceURI, element2.NamespaceURI, StringComparison.Ordinal))
            {
                return false;
            }

            XmlAttribute differentAttribute1, differentAttribute2;
            if (!CompareXmlAttributeCollections(element1.Attributes, element2.Attributes, out differentAttribute1, out differentAttribute2))
            {
                string attributeName1 = differentAttribute1 != null ? "@" + differentAttribute1.LocalName : String.Empty;
                string attributeName2 = differentAttribute2 != null ? "@" + differentAttribute2.LocalName : String.Empty;
                differentLocation = CombineTwoNames(attributeName1, attributeName2);
                return false;
            }

            object differentChild1, differentChild2;
            if (!CompareSchemaCollections(element1.ChildNodes, element2.ChildNodes, out differentChild1, out differentChild2, out differentLocation))
            {
                string child1Name = differentChild1 != null ? ((XmlNode)differentChild1).LocalName : String.Empty;
                string child2Name = differentChild2 != null ? ((XmlNode)differentChild2).LocalName : String.Empty;
                differentLocation = CombinePath(CombineTwoNames(child1Name, child2Name), differentLocation);
                return false;
            }
            return true;
        }

        /// <summary>
        /// Compare two text nodes
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static bool CompareXmlTexts(XmlText text1, XmlText text2)
        {
            return String.Equals(text1.Value, text2.Value, StringComparison.Ordinal);
        }

        /// <summary>
        /// Combine two path (similar to xpath) in error messages
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static string CombinePath(string path1, string path2)
        {
            if (String.IsNullOrEmpty(path1))
            {
                return path2;
            }
            else if (String.IsNullOrEmpty(path2))
            {
                return path1;
            }

            return path1 + "/" + path2;
        }

        /// <summary>
        /// Get Name of a top level schema item
        /// </summary>
        /// <param name="item"></param>
        /// <return></return>
        /// <remarks></remarks>
        private static string GetSchemaItemName(XmlSchemaObject item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            Type itemType = item.GetType();
            PropertyInfo nameProperty = itemType.GetProperty("Name");
            if (nameProperty != null)
            {
                object nameValue = nameProperty.GetValue(item, new object[] { });
                if (nameValue is string)
                {
                    return (string)nameValue;
                }
                return String.Empty;
            }

            return String.Empty;
        }

        /// <summary>
        /// Generate end-user unstandable property name -- we will use name in the schema file, but not name in object model
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        private static string GetSchemaPropertyNameInXml(PropertyInfo property, object value1, object value2)
        {
            object[] propertyAttributes = property.GetCustomAttributes(true);
            string name = String.Empty;

            if (propertyAttributes != null)
            {
                string name1 = GetSchemaPropertyNameInXmlHelper(propertyAttributes, value1);
                string name2 = GetSchemaPropertyNameInXmlHelper(propertyAttributes, value2);

                name = CombineTwoNames(name1, name2);
            }

            if (String.IsNullOrEmpty(name))
            {
                Debug.Fail("Why we didn't get a property name with normal routine?");
                name = property.Name;
            }

            return name;
        }

        /// <summary>
        /// Combine names of two properties in error messages
        /// </summary>
        /// <remarks></remarks>
        private static string CombineTwoNames(string name1, string name2)
        {
            string name = String.Empty;
            if (name1.Length > 0)
            {
                if (name2.Length > 0)
                {
                    if (String.Equals(name1, name2, StringComparison.Ordinal))
                    {
                        name = name1;
                    }
                    else
                    {
                        name = name1 + "|" + name2;
                    }
                }
                else
                {
                    name = name1;
                }
            }
            else if (name2.Length > 0)
            {
                name = name2;
            }
            return name;
        }

        /// <summary>
        /// a helper function to generate names
        /// </summary>
        /// <remarks></remarks>
        private static string GetSchemaPropertyNameInXmlHelper(object[] propertyAttributes, object value)
        {
            if (value != null)
            {
                foreach (object attribute in propertyAttributes)
                {
                    if (attribute is System.Xml.Serialization.XmlAttributeAttribute)
                    {
                        return "@" + ((System.Xml.Serialization.XmlAttributeAttribute)attribute).AttributeName;
                    }
                    if (attribute is System.Xml.Serialization.XmlElementAttribute)
                    {
                        System.Xml.Serialization.XmlElementAttribute elementAttribute = (System.Xml.Serialization.XmlElementAttribute)attribute;
                        Type elementType = elementAttribute.Type;
                        if (elementType == null || elementType.IsInstanceOfType(value))
                        {
                            if (value is XmlSchemaObject)
                            {
                                string itemName = GetSchemaItemName((XmlSchemaObject)value);
                                if (itemName.Length > 0)
                                {
                                    return String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}[@name='{1}']", elementAttribute.ElementName, itemName);
                                }
                            }
                            return elementAttribute.ElementName;
                        }
                    }
                    if (attribute is System.Xml.Serialization.XmlAnyAttributeAttribute)
                    {
                        if (value is XmlAttribute)
                        {
                            return "@" + ((XmlAttribute)value).LocalName;
                        }
                    }
                    if (attribute is System.Xml.Serialization.XmlAnyElementAttribute)
                    {
                        if (value is XmlElement)
                        {
                            return ((XmlElement)value).LocalName;
                        }
                    }
                    if (attribute is System.Xml.Serialization.XmlTextAttribute)
                    {
                        if (value is XmlText)
                        {
                            return ((XmlText)value).Name;
                        }
                    }
                }
            }

            return String.Empty;
        }

        /// <summary>
        /// Check whether a property is persisted with XmlSerialization
        /// </summary>
        /// <param name="property"></param>
        /// <return></return>
        /// <remarks></remarks>
        private static bool IsPersistedProperty(PropertyInfo property)
        {
            object[] propertyAttributes = property.GetCustomAttributes(true);
            if (propertyAttributes != null)
            {
                foreach (object attribute in propertyAttributes)
                {
                    foreach (Type serializationAttibuteType in xmlSerializationAttributes)
                    {
                        if (serializationAttibuteType.IsInstanceOfType(attribute))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// check whether we should report warning but not error messages, when the property is different
        /// </summary>
        /// <remarks></remarks>
        private static bool ShouldIgnoreSchemaProperty(PropertyInfo property)
        {
            Type propertyType = property.PropertyType;
            foreach (Type ignoreableType in ignorablePropertyTypes)
            {
                if (propertyType == ignoreableType || propertyType.IsSubclassOf(ignoreableType))
                {
                    return true;
                }
            }

            // special case constraints...
            if (String.Equals(property.Name, "Constraints", StringComparison.Ordinal))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// a helper structure to hold top level items we want to scan
        /// </summary>
        /// <remarks></remarks>
        private struct SchemaTopLevelItemType
        {
            public Type ItemType;
            public string Name;

            public SchemaTopLevelItemType(Type itemType, string name)
            {
                this.ItemType = itemType;
                this.Name = name;
            }
        };

        /// <summary>
        /// Helper class to compare two XmlAttributes
        /// </summary>
        /// <remarks></remarks>
        private class AttributeComparer : System.Collections.Generic.IComparer<XmlAttribute>
        {

            public int Compare(System.Xml.XmlAttribute x, System.Xml.XmlAttribute y)
            {
                int namespaceResult = String.Compare(x.NamespaceURI, y.NamespaceURI, StringComparison.Ordinal);
                if (namespaceResult != 0)
                {
                    return namespaceResult;
                }

                return String.Compare(x.Name, y.Name, StringComparison.Ordinal);
            }
        }
    }
}