I was hoping to do the generation automatically and not have to change the
class names after the fact, as with anything the XML is a constantly
changing through the development process, and to have to change the classes
each time the code is regenerated would be time consuming.
What I have found though is that if I create Address as a global complex
type with in my schema then it will name it the way I wanted.
Thanks
Wayne
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:dw*************@cpmsftngxa10.phx.gbl...
Hi Wayne,
Thanks for your posting. From your description, you're trying to generate
a c# class which will represent a certain XML format data and is wondering
some approach to customize the class (or property name) that
autogenerated, yes?
I'm not sure whether you've tried the general .net XML Serialization
means, but based on my researh the XmlCodeExporter is mostly for internal used in
net framework. The .net framework's System.Xml.Serialization namespace
has provided plentiful xml attributes that help customize our .net
class(for serializing and deserializing xml data). And there is a existing
tool xsd.exe which can help generate xml schema file and class file from
a given demo xml file. For example:
given a following xml file:
<ROOT>
<ADDRESS>
<CITY>Jacksonville</CITY>
<STATE>FL</STATE>
</ADDRESS>
<ADDRESS>
<CITY>Mike Lon</CITY>
<STATE>FL</STATE>
</ADDRESS>
</ROOT>
by default, when we use the xsd.exe tool, it'll generate the following
classes:
[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class ROOT {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ADDR ESS",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ROOTADDRESS[] Items;
}
/// <remarks/>
public class ROOTADDRESS {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche maForm.Unqualified)]
public string CITY;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche maForm.Unqualified)]
public string STATE;
}
Then, if you want to use other names for the classes, just change the
ROOTADDRESS to ADDRESS
and also change the member type in the ROOT class accordingly. For
example:
just like :
[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class ROOT {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ADDR ESS",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ADDRESS[] Items;
}
/// <remarks/>
public class ADDRESS {
....................
}
After that, we can still use the changed classes to serialize and
deserialize the xml datas :
static void TestSerialize()
{
ADDRESS[] addrs = new ADDRESS[5];
for(int i=0;i<addrs.Length;i++)
{
ADDRESS addr = new ADDRESS();
addr.CITY = "CITY_" + i;
addr.STATE = "STATE_" + i;
addrs[i] = addr;
}
ROOT root = new ROOT();
root.Items = addrs;
XmlSerializer serializer = new XmlSerializer(typeof(ROOT));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
StringWriter sw= new StringWriter(sb);
serializer.Serialize(sw,root);
Console.WriteLine(sb.ToString());
}
HTH. Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)