Click to See Complete Forum and Search --> : Autogenerated XSD.exe code
mharris
July 15th, 2009, 04:41 AM
Hi,
This is more of a .NET question rather than specifically C#, anyway..
I have an XML settings file that stores mappings between fields in two different databases.
<Mappings>
<Mapping name="userName">givenName</Mapping>
<Mapping name="telephone">telephoneNumber</Mapping>
etc...
</Mappings>
I have used XSD.exe to auto generate some classes from the files XSD schema and this results in an array that contains the Mapping objects.
I need to be able to itterate over the list of Mappings so an Array works fine for this. But I also need to access the Mappings via their name so it would be great if these were stored in some sort of dictionary and keyed by the name, eg..
Settings.Mappings["userName"] = "a new value";
Is there any way I can change my schema data structure to provide dictionary type access to the mappings, or can I provide any customized settings to XSD.exe to tell it not to use an array?
Many Thanks,
Martyn.
monalin
July 15th, 2009, 10:43 AM
I generated a class using the xsd.exe file for the following xml document.
<Mappings>
<Mapping name="userName">givenName</Mapping>
<Mapping name="telephone">telephoneNumber</Mapping>
</Mappings>
I modified the class it created to add in a dictionary object. Not sure it works, though it does compile. As far as i know there is no way to have the xsd.exe reconize any form of generic object. The most complex it goes is an array. However as you see i placed the [XmlIgnore()] attribute above the Dictionary property, when its serializing the object it will ignore this field and it won't throw any errors. Let me know if this works for you.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3053
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
//
// This source code was auto-generated by xsd, Version=2.0.50727.1432.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Mappings
{
private MappingsMapping[] mappingField;
private Dictionary<String, String> map = null;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Mapping")]
public MappingsMapping[] Mapping
{
get
{
return this.mappingField;
}
set
{
this.mappingField = value;
}
}
private void InitDictionary()
{
map = new Dictionary<String, String>();
foreach (MappingsMapping m in mappingField)
map[m.name] = m.Value;
}
[XmlIgnore()]
public Dictionary<String, String> Dict
{
get
{
if (map == null)
InitDictionary();
return map;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MappingsMapping
{
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
mharris
July 16th, 2009, 03:10 AM
Hi monalin,
I actually ended up going down a very similar route to the solution you have provided. I needed to keep the auto generated code un-modified because it is likely that this code will be rebuilt regularly, hence any changes would be lost
Fortunately, XSD.exe creates the classes with the 'partial' modifier so I was able to add my customizations to a seperate file, thus allowing the auto generated file to be rebuilt while still keeping the changes.
I added a dictionary property to the class that is a wrapper around the original array and the get/set methods of the dictionary directly access and update the correct element in the array.
Thanks for your help.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.