473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

XmlCodeExporter

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
Nov 16 '05 #1
2 2523
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.)

Nov 16 '05 #2
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.)

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ceri Williams | last post by:
I followed the excellent MSDN article "Code Generation in the .NET Framework Using XML Schema" to build a substitute for the limited xsd.exe. My code works fine under .NET framework 1.1, but after...
0
by: doughboy | last post by:
I have a schema that goes something like this: <xs:element name="account"> <xs:complexType> <xs:sequeuence> <xs:element name="accountid" type="xs:string"/> <xs:element name="account"...
0
by: Jan Jones | last post by:
Hi, I have two XSDs both in seperate namespaces, Child is an extension of the abstract Base: <?xml version="1.0" encoding="utf-8"?> <xs:schema id="Base"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.