public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (CurrentType == null) { ArrayList nodes = Retriever.RetrieveCustomization(element, NodeType.Type); if (nodes.Count != 0 || Retriever.Files.Count == 0) { CodeTypeDeclaration type = new CodeTypeDeclaration(element.TypeName); type.CustomAttributes.AddRange(CodeDomHelper.BuildCustomAttributes(nodes)); type.BaseTypes.AddRange(CodeDomHelper.BuildBaseTypes(nodes)); // Append unhandled attributes to the type UserData property, for use by custom visitors. XmlAttribute[] attributes = element.SchemaObject.UnhandledAttributes; if (attributes != null) { foreach (XmlAttribute attr in attributes) { type.UserData.Add(attr.LocalName, attr); } } CurrentNamespace.Types.Add(type); } } }
/// <summary> /// Visitor implementation. Processes the passed element /// adding the member elements defined in the customization. /// </summary> /// <param name="element"></param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); #region Add Members to Type declarations if (CurrentType != null) { ArrayList nodes = Retriever.RetrieveCustomization( element, NodeType.Type); ParseNodes(nodes, element.Name); } #endregion #region Add Members to Collection declarations string name = element.Name + Configuration.CollectionNaming; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == name) { CurrentType = type; break; } } if (CurrentType != null) { ArrayList nodes = Retriever.RetrieveCustomization( element, NodeType.Collection); ParseNodes(nodes, element.Name); } #endregion }
public override void Visit(VisitableElementComplexType element) { base.Visit(element); // If no type declaration was created for the current // complex type element, exit the process. if (CurrentType == null) { return; } CodeConstructor ctor = null; // Locate existing constructor. foreach (CodeTypeMember member in CurrentType.Members) { if (member is CodeConstructor) { ctor = (CodeConstructor)member; break; } } if (ctor == null) { return; } ctor.Statements.Insert(0, new CodeExpressionStatement( new CodeMethodInvokeExpression( new CodeTypeReferenceExpression( typeof(System.Diagnostics.Debug)), "WriteLine", new CodeExpression[] { new CodePrimitiveExpression( "Constructing object of type " + element.TypeName) }))); }
/// <summary> /// Visitor implementation. Processes the passed element /// adding the corresponding constructors. /// </summary> /// <param name="element"></param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (CurrentType != null) { AddConstructor(element, Retriever.RetrieveCustomization( element, NodeType.Type)); } ArrayList nodes = Retriever.RetrieveCustomization( element, NodeType.Collection); string name = element.Name + Configuration.CollectionNaming; if (nodes.Count != 0) { CurrentType = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == name) { CurrentType = type; break; } } if (CurrentType != null) { AddConstructor(element, nodes); } // TODO: Constructor isn't added!!! } }
/// <summary> /// Visitor implementation. /// </summary> public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (IsDataSet) { // Add subtypes. foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name != CurrentDataSetType.Name && type.Name != CurrentDataSetType.Name + Configuration.TypeNaming) { CurrentDataSetType.Members.Add(type); } } // Remove types from the namespace. CodeTypeDeclaration[] types = new CodeTypeDeclaration[CurrentNamespace.Types.Count]; CurrentNamespace.Types.CopyTo(types, 0); foreach (CodeTypeDeclaration type in types) { if (type.Name != CurrentDataSetType.Name) { CurrentNamespace.Types.Remove(type); } } } }
public void Visit(VisitableElementComplexType element) { //Create a class declaration and add it to the namespace. CodeTypeDeclaration type = new CodeTypeDeclaration(element.TypeName); _currentns.Types.Add(type); }
/// <summary> /// Reposition the CurrentType variable to point to the DataTable object. /// </summary> /// <param name="element"></param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); CurrentType = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == element.Name + Configuration.CollectionNaming) { CurrentType = type; break; } } }
/// <summary> /// Visitor implementation. /// </summary> public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (!IsDataSet) { #region Public DataTable accessors //[System.ComponentModel.Browsable(false)] //[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)] //public publishersDataTable publishers //{ // get { return (publishersDataTable)RetrieveDataTable(typeof(publishersDataTable), "publishers"); } //} CodeMemberProperty prop = new CodeMemberProperty(); prop.Name = element.Name; prop.Type = new CodeTypeReference(element.Name + Configuration.CollectionNaming); prop.HasGet = true; prop.GetStatements.Add( new CodeMethodReturnStatement( new CodeCastExpression( element.Name + Configuration.CollectionNaming, new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "RetrieveDataTable", new CodeExpression[] { new CodeTypeOfExpression(element.Name + Configuration.CollectionNaming), new CodePrimitiveExpression(element.Name) })))); prop.CustomAttributes.Add( new CodeAttributeDeclaration( "System.ComponentModel.Browsable", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(false)) })); prop.CustomAttributes.Add( new CodeAttributeDeclaration( "System.ComponentModel.DesignerSerializationVisibilityAttribute", new CodeAttributeArgument[] { new CodeAttributeArgument( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression( typeof(System.ComponentModel.DesignerSerializationVisibility)), "Content")) })); CurrentDataSetType.Members.Add(prop); #endregion } }
/// <summary> /// Adds constructors to the CurrentType based on the configuration nodes passed. /// </summary> /// <param name="element">Current element.</param> /// <param name="nodes">Configuration nodes to use.</param> private void AddConstructor(VisitableElementComplexType element, ArrayList nodes) { CodeTypeMemberCollection constructors = new CodeTypeMemberCollection(); foreach (XmlNode node in nodes) { foreach (XmlNode inner in node.ChildNodes) { if (inner.LocalName == "Constructor") { CodeConstructor ct = new CodeConstructor(); // TODO: Review if it is necessary to add an attribute to specify visibility. ct.Attributes = MemberAttributes.Public; foreach (XmlNode child in inner.ChildNodes) { if (child.LocalName == "Parameter") { ct.Parameters.Add(new CodeParameterDeclarationExpression( child.Attributes["Type"].InnerText, child.Attributes["Name"].InnerText)); } else if (child.LocalName == "SourceCode") { ct.Statements.Add(new CodeSnippetStatement( CodeDomHelper.ParseCodeContainer(element.Name, Configuration.TypeNaming, Configuration.CollectionNaming, child, CurrentNamespace))); } } constructors.Add(ct); } } } // If we don't find any constructors in the configuration files, we // add a public constructor with no parameters. if (constructors.Count != 0) { CurrentType.Members.AddRange(constructors); } else { CodeConstructor ct = new CodeConstructor(); ct.Attributes = MemberAttributes.Public; CurrentType.Members.Add(ct); } }
/// <summary> /// Visitor implementation. Processes the passed element /// and sets the IsDataSet flag if appropiate. /// </summary> /// <param name="element"></param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); IsDataSet = false; // Search for the IsDataSet attribute. XmlAttribute[] attributes = element.SchemaObject.UnhandledAttributes; if (attributes != null) { foreach (XmlAttribute attr in attributes) { if (attr.LocalName == "IsDataSet" && attr.Value == "true") { IsDataSet = true; } } } }
/// <summary> /// Visitor implementation. Processes the passed element /// repositioning the CurrentType member variable. /// </summary> /// <param name="element"></param> public virtual void Visit(VisitableElementComplexType element) { CurrentType = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == element.TypeName) { CurrentType = type; break; } else { CodeTypeDeclaration inner = RecurseMembers(type, element.TypeName); if (inner != null) { CurrentType = inner; break; } } } }
/// <summary> /// Visitor implementation. Creates the constraints. /// </summary> /// <param name="element"></param> public override void Visit(VisitableElementComplexType element) { // The base visitor will initialize the variables. base.Visit(element); if (!IsDataSet) { // Reset the variables we will use. CurrentType = null; string name = element.Name + Configuration.CollectionNaming; // Find the DataTable type. foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == name) { CurrentType = type; break; } } if (CurrentType != null) { string xpath; bool haspk = false; bool ispk = false; CodeMemberMethod initclass = RetrieveMethod(CurrentType, "InitClass"); CodeMemberMethod initvars = RetrieveMethod(CurrentType, "InitVars"); // Make the InitVars method internal initvars.Attributes = MemberAttributes.Final | MemberAttributes.Assembly; #region Process uniques. xpath = ".//"; xpath += RetrievePrefix(); xpath += element.Name; xpath = "//xsd:unique[xsd:selector/@xpath=\"" + xpath + "\"]"; XmlNodeList uniques = CurrentSchemaDom.SelectNodes(xpath, Namespaces); foreach (XmlNode unique in uniques) { ProcessUniqueKey(unique, initclass, ref haspk, ref ispk); } #endregion #region Process keys. xpath = ".//"; xpath += RetrievePrefix(); xpath += element.Name; xpath = "//xsd:key[xsd:selector/@xpath=\"" + xpath + "\"]"; XmlNodeList keys = CurrentSchemaDom.SelectNodes(xpath, Namespaces); foreach (XmlNode key in keys) { ProcessUniqueKey(key, initclass, ref haspk, ref ispk); } #endregion // Add an internal PK if this element has a nested DataTable and // this table doesn't have a primary key already defined. // TODO: unimplemented. // if (!haspk) // CodeDomHelper.BuildInternalPrimaryKey(CurrentType, element, initclass, initvars); } } }
/// <summary> /// Visitor implementation. /// </summary> /// <param name="element">The element from which to build the code.</param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); // Reposition CurrentType in the DataTable type declaration. CurrentType = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == element.Name + Configuration.CollectionNaming) { CurrentType = type; break; } } if (!IsDataSet) { CodeMemberProperty prop = null; CodeMemberMethod method = null; #region Build DataTable type if it doesn't exist already if (CurrentType == null) { CurrentType = new CodeTypeDeclaration(element.Name + Configuration.CollectionNaming); // Mark the type as being a collection. CurrentType.UserData.Add("IsCollection", true); // Append unhandled attributes to the type UserData property, for use by custom visitors. XmlAttribute[] attributes = element.SchemaObject.UnhandledAttributes; if (attributes != null) { foreach (XmlAttribute attr in attributes) { CurrentType.UserData.Add(attr.LocalName, attr); } } CurrentNamespace.Types.Add(CurrentType); } #endregion CurrentType.BaseTypes.Add( typeof(NMatrix.XDL.BaseDataTable)); #region Constructors //Remove default constructor CodeTypeMember[] members = new CodeTypeMember[CurrentDataSetType.Members.Count]; CurrentType.Members.CopyTo(members, 0); foreach (CodeTypeMember member in members) { if (member is CodeConstructor) { CurrentType.Members.Remove(member); } } //public publishersDataTable(DataTable table) : base(table) CodeConstructor ctor = new CodeConstructor(); ctor.Attributes = MemberAttributes.Public; ctor.Parameters.Add( new CodeParameterDeclarationExpression( typeof(DataTable), "table")); ctor.BaseConstructorArgs.Add( new CodeVariableReferenceExpression("table")); CurrentType.Members.Add(ctor); #endregion #region Indexer //public publishersRow this[int index] //{ // get { return ((publishersRow)(this.Rows[index])); } //} prop = new CodeMemberProperty(); prop.Name = "Item"; prop.Type = new CodeTypeReference(element.TypeName); prop.Parameters.Add( new CodeParameterDeclarationExpression( typeof(int), "index")); prop.HasGet = true; prop.GetStatements.Add( new CodeMethodReturnStatement( new CodeCastExpression( element.TypeName, new CodeIndexerExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Rows"), new CodeExpression[] { new CodeVariableReferenceExpression("index") })))); CurrentType.Members.Add(prop); #endregion #region Events #region Declarations //public event publishersRowChangeEventHandler publishersRowChanged; CodeMemberEvent ev = new CodeMemberEvent(); ev.Attributes = MemberAttributes.Public; ev.Name = element.TypeName + "Changed"; ev.Type = new CodeTypeReference(element.TypeName + "ChangeEventHandler"); CurrentType.Members.Add(ev); //public event publishersRowChangeEventHandler publishersRowChanging; ev = new CodeMemberEvent(); ev.Attributes = MemberAttributes.Public; ev.Name = element.TypeName + "Changing"; ev.Type = new CodeTypeReference(element.TypeName + "ChangeEventHandler"); CurrentType.Members.Add(ev); //public event publishersRowChangeEventHandler publishersRowDeleted; ev = new CodeMemberEvent(); ev.Attributes = MemberAttributes.Public; ev.Name = element.TypeName + "Deleted"; ev.Type = new CodeTypeReference(element.TypeName + "ChangeEventHandler"); CurrentType.Members.Add(ev); //public event publishersRowChangeEventHandler publishersRowDeleting; ev = new CodeMemberEvent(); ev.Attributes = MemberAttributes.Public; ev.Name = element.TypeName + "Deleting"; ev.Type = new CodeTypeReference(element.TypeName + "ChangeEventHandler"); CurrentType.Members.Add(ev); #endregion #region Raising methods CurrentType.Members.Add(OnEvent(element.TypeName, "Changed")); CurrentType.Members.Add(OnEvent(element.TypeName, "Changing")); CurrentType.Members.Add(OnEvent(element.TypeName, "Deleted")); CurrentType.Members.Add(OnEvent(element.TypeName, "Deleting")); #endregion #endregion #region Row-related methods //public publishersRow Add(string pub_id, string pub_name, string city, string state, string country) method = new CodeMemberMethod(); method.Name = "Add"; method.ReturnType = new CodeTypeReference(element.TypeName); CodeTypeDeclaration rowtype = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == element.TypeName) { rowtype = type; break; } } if (rowtype == null) { throw new InvalidOperationException("Type " + element.TypeName + " not found in generated code"); } foreach (CodeTypeMember member in rowtype.Members) { if (member is CodeMemberProperty) { method.Parameters.Add( new CodeParameterDeclarationExpression( ((CodeMemberProperty)member).Type, member.Name)); } } // publishersRow row = this.NewRow(); method.Statements.Add( new CodeVariableDeclarationStatement( element.TypeName, "row", new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "NewRow", new CodeExpression[0]))); // row.ItemArray = new object[] // { pub_id, pub_name, city, state, country }; CodeArrayCreateExpression create = new CodeArrayCreateExpression(); create.CreateType = new CodeTypeReference(typeof(object)); foreach (CodeTypeMember member in rowtype.Members) { if (member is CodeMemberProperty) { create.Initializers.Add(new CodeVariableReferenceExpression(member.Name)); } } method.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression( "row"), "ItemArray"), create)); // this.Rows.Add(row); method.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Rows"), "Add", new CodeExpression[] { new CodeVariableReferenceExpression("row") })); // return row; method.Statements.Add( new CodeMethodReturnStatement(new CodeVariableReferenceExpression("row"))); method.Attributes = MemberAttributes.Public | MemberAttributes.Final; CurrentType.Members.Add(method); //public new publishersRow NewRow() //{ // return ((publishersRow)(base.NewRow())); //} method = new CodeMemberMethod(); method.Name = "NewRow"; method.ReturnType = new CodeTypeReference(element.TypeName); method.Statements.Add( new CodeMethodReturnStatement( new CodeCastExpression( element.TypeName, new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "NewRow", new CodeExpression[0])))); method.Attributes = MemberAttributes.Public | MemberAttributes.New; CurrentType.Members.Add(method); //protected override Type GetRowType() //{ // return typeof(publishersRow); //} method = new CodeMemberMethod(); method.Name = "GetRowType"; method.ReturnType = new CodeTypeReference(typeof(Type)); method.Statements.Add( new CodeMethodReturnStatement( new CodeTypeOfExpression(element.TypeName))); method.Attributes = MemberAttributes.Family | MemberAttributes.Override; CurrentType.Members.Add(method); //public void Remove(publishersRow row) //{ // this.Rows.Remove(row); //} method = new CodeMemberMethod(); method.Name = "Remove"; method.Attributes = MemberAttributes.Public | MemberAttributes.Final; method.Parameters.Add( new CodeParameterDeclarationExpression(element.TypeName, "row")); method.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Rows"), "Remove", new CodeExpression[] { new CodeVariableReferenceExpression("row") })); CurrentType.Members.Add(method); //public void Remove(DataRow row) //{ // this.Rows.Remove(row); //} method = new CodeMemberMethod(); method.Name = "Remove"; method.Attributes = MemberAttributes.Public | MemberAttributes.Final; method.Parameters.Add( new CodeParameterDeclarationExpression(typeof(DataRow), "row")); method.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Rows"), "Remove", new CodeExpression[] { new CodeVariableReferenceExpression("row") })); CurrentType.Members.Add(method); #endregion } }
public override void Visit(VisitableElementComplexType element) { ArrayList nodes = Retriever.RetrieveCustomization(element, NodeType.Collection); string name = element.Name + Configuration.CollectionNaming; if (nodes.Count != 0) { CodeTypeDeclaration type = null; foreach (CodeTypeDeclaration obj in CurrentNamespace.Types) { if (obj.Name == name) { type = obj; break; } } if (type == null) { type = new CodeTypeDeclaration(name); CurrentNamespace.Types.Add(type); // Mark the type as being a collection. type.UserData.Add("IsCollection", true); // Append unhandled attributes to the type UserData property, for use by custom visitors. XmlAttribute[] attributes = element.SchemaObject.UnhandledAttributes; if (attributes != null) { foreach (XmlAttribute attr in attributes) { type.UserData.Add(attr.LocalName, attr); } } } type.CustomAttributes.AddRange(CodeDomHelper.BuildCustomAttributes(nodes)); type.BaseTypes.AddRange(CodeDomHelper.BuildBaseTypes(nodes)); } // If the element is contained in another element, and generateContainerProperty configuration // is "true", add the corresponding property if it isn't already present. if (Configuration.InnerData.GenerateContainerProperty && element.Parent != null && element.Parent is VisitableElementComplexType) { CodeTypeDeclaration enclosing = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == element.Parent.TypeName) { enclosing = type; break; } } // If we find the parent type and the property hasn't been defined already if (enclosing != null) { bool existing = false; foreach (CodeTypeMember prop in enclosing.Members) { if (prop.Name == element.Name) { existing = true; break; } } if (!existing) { CodeMemberProperty prop = new CodeMemberProperty(); prop.Name = element.Name; prop.Attributes = MemberAttributes.Public; // TODO: Review if it is necessary to add an attribute to specify getter and setter for the property. prop.HasGet = true; prop.HasSet = true; // Do we have a custom Collection? if (nodes.Count != 0) { prop.Type = new CodeTypeReference(element.Name + Configuration.CollectionNaming); } else { prop.Type = new CodeTypeReference(element.TypeName, 1); } enclosing.Members.Add(prop); } } } }
/// <summary> /// Visitor implementation. /// </summary> /// <param name="element">The element from which to build the code.</param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (!IsDataSet) { #region Delegate declaration CodeTypeDelegate del = new CodeTypeDelegate(); del.Name = element.TypeName + "ChangeEventHandler"; //param object sender del.Parameters.Add( new CodeParameterDeclarationExpression( typeof(object), "sender")); //param publishersRowChangeEvent e del.Parameters.Add( new CodeParameterDeclarationExpression( element.TypeName + "ChangeEvent", "e")); //public delegate void publishersRowChangeEventHandler(object sender, publishersRowChangeEvent e); CurrentDataSetType.Members.Add(del); #endregion #region Event argument class CodeTypeDeclaration ev = new CodeTypeDeclaration(element.TypeName + "ChangeEvent"); //[System.Diagnostics.DebuggerStepThrough()] ev.CustomAttributes.Add( new CodeAttributeDeclaration("System.Diagnostics.DebuggerStepThrough")); //public class publishersRowChangeEvent : EventArgs { ev.BaseTypes.Add(typeof(EventArgs)); // private publishersRow eventRow; ev.Members.Add(new CodeMemberField(element.TypeName, "eventRow")); // private DataRowAction eventAction; ev.Members.Add(new CodeMemberField(typeof(DataRowAction), "eventAction")); #region Constructor // public publishersRowChangeEvent(publishersRow row, DataRowAction action) CodeConstructor ctor = new CodeConstructor(); ctor.Parameters.Add( new CodeParameterDeclarationExpression(element.TypeName, "row")); ctor.Parameters.Add( new CodeParameterDeclarationExpression(typeof(DataRowAction), "action")); ctor.Attributes = MemberAttributes.Public; // this.eventRow = row; ctor.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "eventRow"), new CodeVariableReferenceExpression("row"))); // this.eventAction = action; ctor.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "eventAction"), new CodeVariableReferenceExpression("action"))); ev.Members.Add(ctor); #endregion #region Properties // public publishersRow Row // get { // return this.eventRow; // } // } CodeMemberProperty prop = new CodeMemberProperty(); prop.Name = "Row"; prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; prop.Type = new CodeTypeReference(element.TypeName); prop.HasGet = true; prop.GetStatements.Add( new CodeMethodReturnStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "eventRow"))); ev.Members.Add(prop); // public DataRowAction Action { // get { // return this.eventAction; // } prop = new CodeMemberProperty(); prop.Name = "Action"; prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; prop.Type = new CodeTypeReference(typeof(DataRowAction)); prop.HasGet = true; prop.GetStatements.Add( new CodeMethodReturnStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "eventAction"))); ev.Members.Add(prop); #endregion CurrentDataSetType.Members.Add(ev); #endregion } }
public override void Visit(VisitableElementComplexType element) { base.Visit(element); // If the element is contained in another element, add the type as a subtype. if (element.Parent != null && element.Parent is VisitableElementComplexType) { string name = element.Parent.TypeName; XmlAttribute[] attributes = ((VisitableElementComplexType)element.Parent).SchemaObject.UnhandledAttributes; // If the IsDataSet attribute is true, don't add the TypeNaming to the type. if (attributes != null) { foreach (XmlAttribute attr in attributes) { if (attr.LocalName == "IsDataSet" && attr.Value == "true") { name = element.Parent.Name; } } } CodeTypeDeclaration enclosing = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == name) { enclosing = type; break; } else { enclosing = RecurseMembers(type, name); if (enclosing != null) { break; } } } // If we find the parent type... if (enclosing != null && CurrentType != null) { CurrentNamespace.Types.Remove(CurrentType); enclosing.Members.Add(CurrentType); } // If there is an enclosing type, move the corresponding collection type too. if (enclosing != null) { CodeTypeDeclaration collection = null; foreach (CodeTypeDeclaration type in CurrentNamespace.Types) { if (type.Name == element.Name + Configuration.CollectionNaming) { collection = type; break; } else { collection = RecurseMembers(type, element.Name + Configuration.CollectionNaming); if (collection != null) { break; } } } if (collection != null) { CurrentNamespace.Types.Remove(collection); enclosing.Members.Add(collection); } } } base.Visit(element); }
/// <summary> /// Adds the inheritance, constructors and appropiate overrides for the generated class. /// </summary> /// <param name="element">The element from which to build the class.</param> public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (!IsDataSet) { CurrentType.BaseTypes.Add(typeof(NMatrix.XDL.BaseRow)); #region Constructors // Remove default constructor. CodeTypeMember[] members = new CodeTypeMember[CurrentType.Members.Count]; CurrentType.Members.CopyTo(members, 0); foreach (CodeTypeMember member in members) { if (member is CodeConstructor) { CurrentType.Members.Remove(member); } } //internal publishersRow(DataRow row) : base(row) CodeConstructor ctor = new CodeConstructor(); ctor.Attributes = MemberAttributes.Assembly; ctor.Parameters.Add( new CodeParameterDeclarationExpression(typeof(DataRow), "row")); ctor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("row")); CurrentType.Members.Add(ctor); //internal publishersRow() ctor = new CodeConstructor(); ctor.Attributes = MemberAttributes.Assembly; CurrentType.Members.Add(ctor); #endregion //protected override Type GetRowType() //{ // return typeof(publishersRow); //} CodeMemberMethod method = new CodeMemberMethod(); method.Name = "GetRowType"; method.Attributes = MemberAttributes.Family | MemberAttributes.Override; method.ReturnType = new CodeTypeReference(typeof(Type)); method.Statements.Add( new CodeMethodReturnStatement( new CodeTypeOfExpression(element.TypeName))); CurrentType.Members.Add(method); //protected override Type GetTableType() //{ // return typeof(publishersDataTable); //} method = new CodeMemberMethod(); method.Name = "GetTableType"; method.Attributes = MemberAttributes.Family | MemberAttributes.Override; method.ReturnType = new CodeTypeReference(typeof(Type)); method.Statements.Add( new CodeMethodReturnStatement( new CodeTypeOfExpression(element.Name + Configuration.CollectionNaming))); CurrentType.Members.Add(method); } }
/// <summary> /// Add the statements required to define a hidden primary key to a DataTable. This /// is needed when no primary key is defined for it, and is used for internal purposes by the /// DataTable object. Adds the variable declaration, internal property accessor and /// statements in InitClass and InitVars methods. /// </summary> /// <param name="currentType">The current DataTable corresponding to the element being visited.</param> /// <param name="element">The element being visited.</param> /// <param name="initClassMethod">The InitClass method.</param> /// <param name="initVarsMethod">The InitVars method.</param> public static void BuildInternalPrimaryKey(CodeTypeDeclaration currentType, VisitableElementComplexType element, CodeMemberMethod initClassMethod, CodeMemberMethod initVarsMethod) { // Add this statement: private DataColumn columntitles_Id; CodeMemberField var = new CodeMemberField(typeof(DataColumn), "column" + element.Name + "_ID"); currentType.Members.Add(var); // Build this statement: // internal DataColumn titles_IDColumn { // get { return this.columntitles_ID; } // } CodeMemberProperty prop = new CodeMemberProperty(); prop.Name = element.Name + "_IDColumn"; prop.Type = new CodeTypeReference(typeof(DataColumn)); prop.Attributes = MemberAttributes.Assembly | MemberAttributes.Final; prop.HasSet = false; prop.HasGet = true; prop.GetStatements.Add( new CodeMethodReturnStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "column" + element.Name + "_ID"))); currentType.Members.Add(prop); // Add the following statement to InitVars: // this.columntitles_ID = this.Columns["titles_ID"]; CodeIndexerExpression column = new CodeIndexerExpression(); column.TargetObject = new CodeThisReferenceExpression(); column.Indices.Add(new CodePrimitiveExpression(element.Name + "_ID")); initVarsMethod.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "column" + element.Name + "_ID"), new CodeIndexerExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Columns"), new CodeExpression[] { new CodePrimitiveExpression(element.Name + "_ID") }))); // Add the following statements to InitClass: // this.columntitles_Id = new DataColumn("titles_ID", // typeof(int), null, System.Data.MappingType.Hidden); // this.Columns.Add(this.columntitles_ID); // this.columntitles_ID.AutoIncrement = true; // this.columntitles_ID.AllowDBNull = false; // this.columntitles_ID.Unique = true; CodeExpression[] prm = new CodeExpression[4]; prm[0] = new CodePrimitiveExpression(element.Name + "_ID"); prm[1] = new CodeTypeOfExpression(typeof(int)); prm[2] = new CodePrimitiveExpression(null); //prm[3] = new CodePrimitiveExpression(System.Data.MappingType.Hidden); prm[3] = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(System.Data.MappingType)), "Element"); // Save a reference to: this.columntitles_ID CodePropertyReferenceExpression idcol = new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "column" + element.Name + "_ID"); initClassMethod.Statements.Add( new CodeAssignStatement(idcol, new CodeObjectCreateExpression(typeof(DataColumn), prm))); initClassMethod.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Columns"), "Add", new CodeExpression[] { idcol })); initClassMethod.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression(idcol, "AutoIncrement"), new CodePrimitiveExpression(true))); initClassMethod.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression(idcol, "AllowDBNull"), new CodePrimitiveExpression(false))); initClassMethod.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression(idcol, "Unique"), new CodePrimitiveExpression(true))); }
/// <summary> /// Visitor implementation. Adds code to InitClass and InitVars methods of the DataTable classes. /// </summary> /// <param name="element"></param> public override void Visit(VisitableElementComplexType element) { // The base visitor will initialize the variables. base.Visit(element); if (!IsDataSet) { string xpath; ArrayList fieldsref = new ArrayList(); ArrayList fieldskey = new ArrayList(); CodeMemberMethod initclass = RetrieveMethod(CurrentDataSetType, "InitClass"); CodeMemberMethod initvars = RetrieveMethod(CurrentDataSetType, "InitVars"); #region Initialize InitVars method. //this.tablepublishers = ((publishersDataTable)(this.Tables["publishers"])); initvars.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name), new CodeCastExpression(element.Name + Configuration.CollectionNaming, new CodeIndexerExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Tables"), new CodeExpression[] { new CodePrimitiveExpression(element.Name) })))); //if ((this.tablepublishers != null)) this.tablepublishers.InitVars(); initvars.Statements.Add( new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name), CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)), new CodeStatement[] { new CodeExpressionStatement( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name), "InitVars", new CodeExpression[0])) })); #endregion #region Initialize InitClass method. //this.tablepublishers = new publishersDataTable(); initclass.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name), new CodeObjectCreateExpression( element.Name + Configuration.CollectionNaming, new CodeExpression[0]))); //this.Tables.Add(this.tablepublishers); initclass.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Tables"), "Add", new CodeExpression[] { new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name) })); #endregion #region Retrieve keyref nodes. xpath = ".//"; xpath += RetrievePrefix(); xpath += element.Name; xpath = "//xsd:keyref[xsd:selector/@xpath=\"" + xpath + "\"]"; XmlNodeList keyrefs = CurrentSchemaDom.SelectNodes(xpath, Namespaces); foreach (XmlNode keyref in keyrefs) { string parentname = String.Empty; string fkname = keyref.Attributes.GetNamedItem("name").Value; if (keyref.Attributes.GetNamedItem("ConstraintName") != null) { fkname = keyref.Attributes.GetNamedItem("ConstraintName").Value; } // Retrieve fields which form the keyref foreach (XmlNode node in keyref.ChildNodes) { if (node.LocalName == "field") { fieldsref.Add(Regex.Replace(node.Attributes.GetNamedItem("xpath").Value, "([A-z|0-9]+):", "")); } } //Retrieve the key being referenced and its fields xpath = ".//xsd:key[@name=\"" + keyref.Attributes.GetNamedItem("refer").Value + "\"] | "; xpath += ".//xsd:unique[@name=\"" + keyref.Attributes.GetNamedItem("refer").Value + "\"]"; XmlNode key = CurrentSchemaDom.SelectSingleNode(xpath, Namespaces); if (key == null) { throw new ArgumentException("A referenced key couldn't be found."); } foreach (XmlNode node in key.ChildNodes) { if (node.LocalName == "field") { fieldskey.Add( Regex.Replace(node.Attributes.GetNamedItem("xpath").Value, "([A-z|0-9]+):", ""). Replace(".", "").Replace("/", "")); } else if (node.LocalName == "selector") { parentname = Regex.Replace(node.Attributes.GetNamedItem("xpath").Value, "([A-z|0-9]+):", ""). Replace(".", "").Replace("/", ""); } } #region Create Foreign Key CodeExpression[] fields = new CodeExpression[fieldskey.Count]; for (int i = 0; i < fieldskey.Count; i++) { fields[i] = new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + parentname), fieldskey[i].ToString() + "Column"); } CodeArrayCreateExpression parentfldcreate = new CodeArrayCreateExpression(typeof(DataColumn), fields); fields = new CodeExpression[fieldsref.Count]; for (int i = 0; i < fieldsref.Count; i++) { fields[i] = new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name), fieldsref[i].ToString() + "Column"); } CodeArrayCreateExpression childfldcreate = new CodeArrayCreateExpression(typeof(DataColumn), fields); //fkc = new ForeignKeyConstraint("publisherstitles", new DataColumn[] { // this.tablepublishers.pub_idColumn, // this.tablepublishers.pub_nameColumn}, new DataColumn[] { // this.tabletitles.titlepub_idColumn, // this.tabletitles.titleColumn}); initclass.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("fkc"), new CodeObjectCreateExpression(typeof(ForeignKeyConstraint), new CodeExpression[] { new CodePrimitiveExpression(fkname), parentfldcreate, childfldcreate }))); #endregion #region Add and initialize the Foreign Key //this.tabletitles.Constraints.Add(fkc); initclass.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "table" + element.Name), "Constraints"), "Add", new CodeExpression[] { new CodeVariableReferenceExpression("fkc") })); //fkc.AcceptRejectRule = AcceptRejectRule.None; CodeExpression arrule = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(AcceptRejectRule)), "None"); if (keyref.Attributes.GetNamedItem("AcceptRejectRule") != null) { arrule = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(AcceptRejectRule)), keyref.Attributes.GetNamedItem("AcceptRejectRule").Value); } initclass.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("fkc"), "AcceptRejectRule"), arrule)); //fkc.DeleteRule = Rule.Cascade; CodeExpression rule = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(Rule)), "Cascade"); if (keyref.Attributes.GetNamedItem("DeleteRule") != null) { arrule = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(Rule)), keyref.Attributes.GetNamedItem("DeleteRule").Value); } initclass.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("fkc"), "DeleteRule"), rule)); //fkc.UpdateRule = Rule.Cascade; rule = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(Rule)), "Cascade"); if (keyref.Attributes.GetNamedItem("UpdateRule") != null) { arrule = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(Rule)), keyref.Attributes.GetNamedItem("UpdateRule").Value); } initclass.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("fkc"), "UpdateRule"), rule)); #endregion #region Add and initialize the DataRelation if (keyref.Attributes.GetNamedItem("ConstraintOnly") == null || keyref.Attributes.GetNamedItem("ConstraintOnly").Value != "false") { string relation = keyref.Attributes.GetNamedItem("name").Value; //private DataRelation relationpublisherstitles; (at dataset-level) CodeMemberField fld = new CodeMemberField( typeof(DataRelation), "relation" + keyref.Attributes.GetNamedItem("name").Value); fld.Attributes = MemberAttributes.Private; CurrentDataSetType.Members.Add(fld); //In InitVars: //this.relationpublisherstitles = this.Relations["publisherstitles"]; initvars.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "relation" + relation), new CodeIndexerExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Relations"), new CodeExpression[] { new CodePrimitiveExpression(relation) }))); //this.relationpublisherstitles = new DataRelation("publisherstitles", new DataColumn[] { // this.tablepublishers.pub_idColumn, // this.tablepublishers.pub_nameColumn}, new DataColumn[] { // this.tabletitles.titlepub_idColumn, // this.tabletitles.titleColumn}, false); initclass.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "relation" + relation), new CodeObjectCreateExpression( typeof(DataRelation), new CodeExpression[] { new CodePrimitiveExpression(relation), parentfldcreate, childfldcreate }))); //this.relationpublishertitles.Nested = true; if (element.Parent != null && element.Parent is VisitableElementComplexType) { initclass.Statements.Add( new CodeAssignStatement( new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "relation" + relation), "Nested"), new CodePrimitiveExpression(true))); } //this.Relations.Add(this.relationpublisherstitles); initclass.Statements.Add( new CodeMethodInvokeExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Relations"), "Add", new CodeExpression[] { new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "relation" + relation) })); // Add the property to retrieve child rows to the parent type. parentname += Configuration.TypeNaming; string childname = element.Name + Configuration.TypeNaming; foreach (CodeTypeMember type in CurrentNamespace.Types) { if (type.Name == parentname) { //public titlesRow[] GettitlesRows() { // return ((titlesRow[]) // (this.GetChildRows(this.Table.ChildRelations["publisherstitles"]))); } CodeMemberMethod getrows = new CodeMemberMethod(); getrows.Attributes = MemberAttributes.Public | MemberAttributes.Final; getrows.ReturnType = new CodeTypeReference(childname, 0); getrows.Name = "Get" + childname; getrows.Statements.Add( new CodeMethodReturnStatement( new CodeCastExpression(new CodeTypeReference(childname, 0), new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "GetChildRows", new CodeExpression[] { new CodeIndexerExpression( new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Table"), "ChildRelations"), new CodeExpression[] { new CodePrimitiveExpression(relation) }) })))); break; } } } #endregion } #endregion /* * internal void InitVars() { * this.relationtitlestitleauthors = this.Relations["titlestitleauthors"]; * } */ #region Add the Get* method to the parent elements in the relations /* * public titlesRow[] GettitlesRows() * { * return ((titlesRow[])(this.GetChildRows(this.Table.ChildRelations["publisherstitles"]))); * } */ #endregion } }
/// <summary> /// Looks for keys which use the element, and adds parent or child references. /// </summary> /// <param name="element">The element being inspected.</param> /// <remarks>In an xsd schema, the following represents a foreign-key: /// >xsd:keyref name="publisherstitles" refer="publisherKey"< /// >xsd:selector xpath=".//mstns:titles" /< /// >xsd:field xpath="mstns:pub_id" /< /// >/xsd:keyref< /// the "refer" attribute designates the parent key. /// The xsd:selector represents the child element and xsd:field the /// children field which points to the parent key. There may be multiple fields. /// </remarks> public override void Visit(VisitableElementComplexType element) { base.Visit(element); if (!IsDataSet) { string xpath; #region Look for parent relations // We need to look for every keyref where the current element appears in the xsd:selector element xpath = ".//"; xpath += RetrievePrefix(); xpath += element.Name; xpath = ".//xsd:keyref[xsd:selector/@xpath=\"" + xpath + "\"]"; XmlNodeList keyrefs = CurrentSchemaDom.SelectNodes(xpath, Namespaces); foreach (XmlNode keyref in keyrefs) { string parent; xpath = ".//xsd:key[@name=\"" + keyref.Attributes["refer"].Value + "\"] | "; xpath += ".//xsd:unique[@name=\"" + keyref.Attributes["refer"].Value + "\"]"; XmlNode key = CurrentSchemaDom.SelectSingleNode(xpath, Namespaces); XmlNode parentnode = CurrentSchemaDom.SelectSingleNode( "//xsd:element[@name=\"" + Regex.Replace(key.SelectSingleNode("xsd:selector/@xpath", Namespaces).Value, "([A-z|0-9|.|/]+):", "") + "\"]", Namespaces); parent = parentnode.Attributes["name"].Value; //public publishersRow publishers CodeMemberProperty prop = new CodeMemberProperty(); prop.Name = parent; prop.Attributes = MemberAttributes.Public; prop.HasGet = true; prop.HasSet = true; prop.Type = new CodeTypeReference(parent + Configuration.TypeNaming); #region Property Get // get { // return ((publishersRow)GetParentRow("publisherstitles", typeof(publishersRow))); // } prop.GetStatements.Add( //return new CodeMethodReturnStatement( //(publishersRow) new CodeCastExpression( parent + Configuration.TypeNaming, //(GetParentRow new CodeMethodInvokeExpression(null, "GetParentRow", //"publishertitltes" new CodeExpression[] { new CodePrimitiveExpression(keyref.Attributes["name"].Value), //, typeof(publishersRow) new CodeTypeOfExpression(parent + Configuration.TypeNaming) })))); #endregion #region Property Set // set { SetParentRow(value, this.Table.ParentRelations["publisherstitles"]); } prop.SetStatements.Add( //SetParentRow new CodeMethodInvokeExpression(null, "SetParentRow", new CodeExpression[] { //(value new CodeVariableReferenceExpression("value"), //, this.Table.ParentRelations new CodeIndexerExpression( new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeThisReferenceExpression(), "Table"), "ParentRelations"), //["publisherstitles"] new CodePrimitiveExpression(keyref.Attributes["name"].Value)) })); #endregion CurrentType.Members.Add(prop); } #endregion #region Look for child relations // We need to look for every keyref where the current element appears in the "refer" key xpath = ".//"; xpath += RetrievePrefix(); xpath += element.Name; xpath = ".//xsd:key[xsd:selector/@xpath=\"" + xpath + "\"] | " + ".//xsd:unique[xsd:selector/@xpath=\"" + xpath + "\"]"; XmlNodeList keys = CurrentSchemaDom.SelectNodes(xpath, Namespaces); // For each key, try to find a keyref pointing to it. foreach (XmlNode key in keys) { xpath = ".//xsd:keyref[@refer=\"" + key.Attributes["name"].Value + "\"]"; XmlNode keyref = CurrentSchemaDom.SelectSingleNode(xpath, Namespaces); if (keyref != null) { // Convert ".//mstns:titles" to "titles" string child = Regex.Replace( keyref.SelectSingleNode("xsd:selector/@xpath", Namespaces).Value, "([A-z|0-9|.|/]+):", ""); //public titlesRow[] titles() { // return ((titlesRow[])(GetChildRows("publisherstitles", typeof(titlesRow))); //} CodeMemberProperty prop = new CodeMemberProperty(); //titlesRow[] CodeTypeReference type = new CodeTypeReference(child + Configuration.TypeNaming, 1); prop.Name = child; prop.Type = type; prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; prop.HasGet = true; prop.GetStatements.Add( //return new CodeMethodReturnStatement( //(titlesRow[]) new CodeCastExpression(type, new CodeMethodInvokeExpression(null, "GetChildRows", new CodeExpression[] { //"publisherstitles" new CodePrimitiveExpression(keyref.Attributes["name"].Value), new CodeTypeOfExpression(child + Configuration.TypeNaming) })))); CurrentType.Members.Add(prop); } } #endregion } }
public void Visit(VisitableElementComplexType element) { //Can create a table for the type }
/// <summary> /// Retrieves a list containing XmlNode objects with the configurations that apply to /// the element and nodeType (Type or Collection). /// </summary> /// <param name="element">The XmlSchemaElement to filter by.</param> /// <param name="nodeType">Can be eiter "Type" or "Collection", which are the possible /// configuration nodes</param> /// <param name="files">The list of XmlDocuments to use for retrieval.</param> /// <returns>ArrayList with the XmlNode objects matching the criteria.</returns> private ArrayList GetTypeConfiguration(VisitableElementComplexType element, NodeType nodeType, ArrayList files) { ArrayList results = new ArrayList(); StringWriter w = new StringWriter(); XmlAttribute[] attributes = element.SchemaObject.UnhandledAttributes; // TODO: Add the posibility to specify wildcards in ApplyTo/ExceptOf filters. //Open the query w.Write("//xgf:" + nodeType.ToString() + "["); //---- ApplyTo section ----// //Check for /Name w.Write("(not(boolean(xgf:ApplyTo/xgf:Name)) or xgf:ApplyTo/xgf:Name=\"{0}\")", element.Name); //Check for /Attribute if (attributes != null) { foreach (XmlAttribute attr in attributes) { w.Write(" and (not(boolean(xgf:ApplyTo/xgf:Attribute/@Name=\"{0}\"))", attr.Name); w.Write(" or (xgf:ApplyTo/xgf:Attribute/@Name=\"{0}\" and xgf:ApplyTo/xgf:Attribute/@Value=\"{1}\"))", attr.Name, attr.Value); } } //------------------------// //---- ExceptOf section ----// //Check for /Name w.Write(" and not(xgf:ExceptOf/xgf:Name=\"{0}\")", element.Name); //Check for /Attribute if (attributes != null) { foreach (XmlAttribute attr in attributes) { w.Write(" and (not(xgf:ExceptOf/xgf:Attribute/@Name=\"{0}\" and xgf:ExceptOf/xgf:Attribute/@Value=\"{1}\"))", attr.Name, attr.Value); } } //------------------------// //Close the query w.Write("]"); string xpath = w.ToString(); // Runs the xpath query against every file in the list. foreach (XmlDocument doc in files) { XmlNamespaceManager mgr = GetManager(doc); XmlNodeList nodes = doc.SelectNodes(xpath, mgr); bool passed; foreach (XmlNode node in nodes) { // Check that attributes exist if they are present in ApplyTo section XmlNodeList attrnodes = node.SelectNodes("//xgf:" + nodeType.ToString() + "/xgf:ApplyTo/xgf:Attribute", mgr); passed = false; foreach (XmlNode attrnode in attrnodes) { if (attributes != null) { foreach (XmlAttribute attr in attributes) { if (attr.Name == attrnode.Attributes["Name"].Value) { if (attr.Value == attrnode.Attributes["Value"].Value) { passed = true; } else { passed = false; } } } } } // If the attributes have passed or there are no attributes in the ApplyTo section. if (passed || attrnodes.Count == 0) { results.Add(node); } } } return(results); }