public static bool FindAttribute( String targetNamespace, String parentTypeName, String attributeName,
out XmlSchemaAttribute attribute)
{
bool foundAttribute = false;
attribute = null;
XmlSchemaComplexType parentType = null;
XmlSchemaElement parentElement = null;
//---------------------------------------------//
//--- First lets look into the Complex Type ---//
//---------------------------------------------//
if (GetComplexType( targetNamespace, parentTypeName, out parentType ))
{
foreach (object o in parentType.AttributeUses)
{
if (o is DictionaryEntry && ( (DictionaryEntry) o ).Value is XmlSchemaAttribute)
{
var att = (XmlSchemaAttribute) ( (DictionaryEntry) o ).Value;
if (att != null && att.Name != null && att.Name.Equals( attributeName ))
{
attribute = att;
foundAttribute = true;
break;
}
}
}
if (!foundAttribute)
{
Dictionary<string, XmlSchemaObject> xmlSchemaObjects = new Dictionary<string, XmlSchemaObject>();
ExtractAttributes(parentType, xmlSchemaObjects);
if (xmlSchemaObjects.Count > 0)
{
if (xmlSchemaObjects.ContainsKey( attributeName ))
{
attribute = xmlSchemaObjects[attributeName] as XmlSchemaAttribute;
foundAttribute = true;
}
}
}
if (!foundAttribute)
{
XmlSchemaComplexContentExtension ext = parentType.ContentModel.Content as XmlSchemaComplexContentExtension;
Dictionary<string, XmlSchemaObject> xmlSchemaObjects = new Dictionary<string, XmlSchemaObject>();
ExtractAttributes(ext, xmlSchemaObjects);
if (xmlSchemaObjects.Count > 0)
{
if (xmlSchemaObjects.ContainsKey(attributeName))
{
attribute = xmlSchemaObjects[attributeName] as XmlSchemaAttribute;
foundAttribute = true;
}
}
}
}
//------------------------------------------------------------------------------------------//
//--- If the attribute was not found in the Complex Type then lets look into the Element ---//
//------------------------------------------------------------------------------------------//
if (!foundAttribute && GetElement(targetNamespace, parentTypeName, out parentElement))
{
var ct = parentElement.SchemaType as XmlSchemaComplexType;
if (ct != null)
{
if (ct.ContentModel != null)
{
var content = ct.ContentModel.Content as XmlSchemaComplexContentExtension;
if (content != null)
{
foreach (object o in content.Attributes)
{
var att = o as XmlSchemaAttribute;
var attGroupRef = o as XmlSchemaAttributeGroupRef;
if (att != null && att.Name != null && att.Name.Equals( attributeName ))
{
attribute = att;
foundAttribute = true;
break;
}
//-----------------------------------------------------//
//--- Lets look into any Attribute Group References ---//
//-----------------------------------------------------//
if (attGroupRef != null && attGroupRef.RefName != null )
{
XmlSchemaAttributeGroup attributeGroup;
string ns = attGroupRef.RefName.Namespace;
string name = attGroupRef.RefName.Name;
GetAttributeGroup( ns, name, out attributeGroup );
if (attributeGroup != null)
{
foreach (var attribute1 in attributeGroup.Attributes)
{
var att1 = attribute1 as XmlSchemaAttribute;
if (att1 != null && att1.Name != null && att1.Name.Equals( attributeName ))
{
attribute = att1;
foundAttribute = true;
break;
}
}
}
}
}
}
}
}
}
return foundAttribute;
}