Support to XmlSerializerNamespaces and Prefixes

Mar 21, 2009 at 3:41 PM
Edited Mar 21, 2009 at 3:43 PM

I am currently using Xsd2Code in my current assignment project and having some problem of how the namespace prefixes are managed and assigned. This project is using standardized schemas, and legacy compliancy to fixed prefixes to namespaces.

Based on the above I could not find any way to control how the namespace prefixes are assigned to schema when serializing so I added some quick and dirty code, which is just to move forward now.

I would like to see this feature to be added as configurable pair collection (dictionary) of prefixes and namespaces that Serialize method would use.

See my hard coded generation in below, which I added to CodeExtension class as temporary (dirty) workaround.

Regards,
Alexander

This is before where Serializer is created....

// --------------------------------------------------------------------------
// Extension to manage external Namespace prefixes
// Create the namespace prefix list
// --------------------------------------------------------------------------

Dictionary<string,string> nameList = new Dictionary<string,string>();
nameList.Add("xx",http://www.confidentialstandards.com/schema/xx/v2);
nameList.Add("mxx",http://www.confidentialstandards.com/schema/mxx/v2);
nameList.Add("ds","www.w3.org/2000/09/xmldsig#");

// --------------------------------------------------------------------------
// System.Xml.Serialization.XmlSerializerNamespaces nsPrefixList = new System.Xml.Serialization.XmlSerializerNamespaces();
// --------------------------------------------------------------------------
serializeMethod.Statements.Add( new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(XmlSerializerNamespaces)), "nsPrefixList", new CodeObjectCreateExpression( new CodeTypeReference(typeof(XmlSerializerNamespaces)))));

// Extension to add each namespace prefix from the nameList
// --------------------------------------------------------------------------
// will generate following statement based on nsPrefixList collection where prefix is key, and namespace is value of dictionary item.
   nsPrefixList.Add("prefix", "namespace");
// --------------------------------------------------------------------------

foreach(var nsAdd in nameList)
{
  serializeMethod.Statements.Add(
 CodeDomHelper.GetInvokeMethod("nsPrefixList", "Add", new CodeExpression[]
  new CodePrimitiveExpression(nsAdd.Key),new CodePrimitiveExpression(nsAdd.Value)}));
}