XmlCodeExporter

W

Wayne

I currently have some code that loads a Schema, and then exports it as a
class. I have the following XML:

<ROOT>
<ADDRESS>
<CITY>Jacksonville</CITY>
<STATE>FL</STATE>
</ADDRESS>
</ROOT>

I then generate a schema and ADDRESS is a ComplexType, when I generate the
code the class that represents Address is called ROOTADDRESS. I am using
XmlCodeExporter.ExportTypeMapping to do the initial code generation. Is
there a way to have the Address class just be called ADDRESS?

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
S

Steven Cheng[MSFT]

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(Namespace="", IsNullable=false)]
public class ROOT {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ADDRESS",
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(Namespace="", IsNullable=false)]
public class ROOT {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ADDRESS",
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 = 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.)
 
W

Wayne

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 said:
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(Namespace="", IsNullable=false)]
public class ROOT {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ADDRESS",
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(Namespace="", IsNullable=false)]
public class ROOT {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ADDRESS",
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 = 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.)
 
S

Steven Cheng[MSFT]

Hi Wayne,

I think what you found did explain the problem. The codegenerate tool (
xsd.exe) may always treat the nested complextype as a parent depended type
so that add a parent name prefix in the type's class name. And if we make
it a global type and reference it in its parent ElementType that will make
the generated class name without the parent type's prefix.

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top