473,326 Members | 2,113 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,326 software developers and data experts.

class generated from XSD.exe will not generate XML?

Using this command line to run XSD.exe

xsd /c /o:outputDir input.xsd /f

on the following XSD

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:dino="http://example.com/test" targetNamespace="http://example.com/test"
elementFormDefault="qualified">
<element name="dinosaurs">
<complexType>
<attribute name="version" type="decimal" fixed="1.0" />
<attribute name="source" type="normalizedString" />
<attribute name="period" default="Cretaceous">
<simpleType>
<restriction base="string">
<enumeration value="Triassic" />
<enumeration value="Jurassic" />
<enumeration value="Cretaceous" />
</restriction>
</simpleType>
</attribute>
</complexType>
</element>

</schema>

a C# class is created with fields such as:

[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal version;

If I create an XML file from this class using

public static void SaveToXmlFile<T>(T value, string pathName) {
using (TextWriter writer = new StreamWriter(pathName)) {
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writer, value);
}
}

dinosaurs c = new dinosaurs();
c.source = "dog";
c.period = dinosaursPeriod.Cretaceous;
SaveToXmlFile(c, @"z:\testXml.xml");

Not a single field of the class will be written.

If I get rid of the

[System.Xml.Serialization.XmlAttributeAttribute()]

in front of each field, the fields will be written.

What can I do to get XSD to produce a class that will output to an XML file
without modification. Or, is there something I must do to get the class to
output properly with the XmlAttributeAttributes in place?
--
Thanks in advance, Les Caudle
May 7 '07 #1
8 7004
Hi Les

[XmlAttributeAttribute] specifies that the XmlSerializer must serialize the
class member as an XML attribute. If you remove [XmlAttributeAttribute]
attribute, XmlSerializer will serialize the member as an XML element.
#1 Attribute
<dinosaurs ... source="dog" .../>
#2 Element
<dinosaurs ...>
< source>dog </source>
</dinosaurs >
Not a single field of the class will be written.
I'm sorry to say I haven't reproduced the issue on my side so far.
I use "xsd /c /o:outputDir input.xsd /f" to generate input.cs from the xsd
file.

xsd.exe verstion 2.0.50727.42

private void button1_Click(object sender, EventArgs e)
{
dinosaurs c = new dinosaurs();
c.source = "dog";
c.period = dinosaursPeriod.Cretaceous;
SaveToXmlFile<dinosaurs>(c, @"c:\testXml.xml");
}
public static void SaveToXmlFile<T>(T value, string pathName)
{
using (TextWriter writer = new StreamWriter(pathName))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writer, value);
}
}

The xml file output as below

<?xml version="1.0" encoding="utf-8"?>
<dinosaurs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" source="dog"
xmlns="http://example.com/test" />

The "source" attribute has been outputted as [source="dog"]
Am I missing something here? Could you please provide a sample project for
me to reproduce the issue if it is possible for you? My alias is
v-******@microsoft.com

Have a great day,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

//--------------------------------------------------------------------------
----
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.312
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------
----

using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true,
Namespace="http://example.com/test")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://example.com/tes
t", IsNullable=false)]
public partial class dinosaurs {

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal version;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool versionSpecified;

/// <remarks/>

[System.Xml.Serialization.XmlAttributeAttribute(Dat aType="normalizedString")
]
public string source;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]

[System.ComponentModel.DefaultValueAttribute(dinosa ursPeriod.Cretaceous)]
public dinosaursPeriod period;

public dinosaurs() {
this.version = ((decimal)(1.0m));
this.period = dinosaursPeriod.Cretaceous;
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true,
Namespace="http://example.com/test")]
public enum dinosaursPeriod {

/// <remarks/>
Triassic,

/// <remarks/>
Jurassic,

/// <remarks/>
Cretaceous,
}
===========================================

May 7 '07 #2
Wen - your code exactly matches what I was doing.

I'm thinking that my XSD is incorrect.

My goal is to create an XML file from that class that would look like this:

<?xml version="1.0" encoding="utf-8"?>
<dinosaurs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/dinosaurs">
<source>dog</source>
<period>Cretaceous</period>
<version>1.0<version>
</dinosaurs>

So, possibley the XSD I used:

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:dino="http://example.com/test" targetNamespace="http://example.com/test"
elementFormDefault="qualified">
<element name="dinosaurs">
<complexType>
<attribute name="version" type="decimal" fixed="1.0" />
<attribute name="source" type="normalizedString" />
<attribute name="period" default="Cretaceous">
<simpleType>
<restriction base="string">
<enumeration value="Triassic" />
<enumeration value="Jurassic" />
<enumeration value="Cretaceous" />
</restriction>
</simpleType>
</attribute>
</complexType>
</element>
</schema>

needs to be changed so that it produces values in the xml file, not attributes?

Thanks, Les Caudle
On Mon, 07 May 2007 09:36:56 GMT, v-******@online.microsoft.com (WenYuan Wang
[MSFT]) wrote:
>Hi Les

[XmlAttributeAttribute] specifies that the XmlSerializer must serialize the
class member as an XML attribute. If you remove [XmlAttributeAttribute]
attribute, XmlSerializer will serialize the member as an XML element.
#1 Attribute
<dinosaurs ... source="dog" .../>
#2 Element
<dinosaurs ...>
< source>dog </source>
</dinosaurs >
>Not a single field of the class will be written.
I'm sorry to say I haven't reproduced the issue on my side so far.
I use "xsd /c /o:outputDir input.xsd /f" to generate input.cs from the xsd
file.

xsd.exe verstion 2.0.50727.42

private void button1_Click(object sender, EventArgs e)
{
dinosaurs c = new dinosaurs();
c.source = "dog";
c.period = dinosaursPeriod.Cretaceous;
SaveToXmlFile<dinosaurs>(c, @"c:\testXml.xml");
}
public static void SaveToXmlFile<T>(T value, string pathName)
{
using (TextWriter writer = new StreamWriter(pathName))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writer, value);
}
}

The xml file output as below

<?xml version="1.0" encoding="utf-8"?>
<dinosaurs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" source="dog"
xmlns="http://example.com/test" />

The "source" attribute has been outputted as [source="dog"]
Am I missing something here? Could you please provide a sample project for
me to reproduce the issue if it is possible for you? My alias is
v-******@microsoft.com

Have a great day,
Wen Yuan
Microsoft Online Community Support
================================================= =
This posting is provided "AS IS" with no warranties, and confers no rights.

//--------------------------------------------------------------------------
----
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.312
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------
----

using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true,
Namespace="http://example.com/test")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://example.com/tes
t", IsNullable=false)]
public partial class dinosaurs {

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal version;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool versionSpecified;

/// <remarks/>

[System.Xml.Serialization.XmlAttributeAttribute(Dat aType="normalizedString")
]
public string source;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]

[System.ComponentModel.DefaultValueAttribute(dinosa ursPeriod.Cretaceous)]
public dinosaursPeriod period;

public dinosaurs() {
this.version = ((decimal)(1.0m));
this.period = dinosaursPeriod.Cretaceous;
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true,
Namespace="http://example.com/test")]
public enum dinosaursPeriod {

/// <remarks/>
Triassic,

/// <remarks/>
Jurassic,

/// <remarks/>
Cretaceous,
}
===========================================
May 7 '07 #3
Les Caudle wrote:
My goal is to create an XML file from that class that would look like this:

<?xml version="1.0" encoding="utf-8"?>
<dinosaurs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/dinosaurs">
<source>dog</source>
<period>Cretaceous</period>
<version>1.0<version>
</dinosaurs>
So source, period and version are elements, not attributes.

<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:dino="http://example.com/test" targetNamespace="http://example.com/test"
elementFormDefault="qualified">
The namespace above is http://www.example.com/dinosaurs not
http://example.com/test so first thing to change is the targetNamespace
of the schema to e.g.
targetNamespac="http://www.example.com/dinosaurs"
<element name="dinosaurs">
<complexType>
<attribute name="version" type="decimal" fixed="1.0" />
<attribute name="source" type="normalizedString" />
<attribute name="period" default="Cretaceous">
<simpleType>
<restriction base="string">
<enumeration value="Triassic" />
<enumeration value="Jurassic" />
<enumeration value="Cretaceous" />
</restriction>
</simpleType>
</attribute>
</complexType>
Then inside of the complexType define a sequence e.g.

<element name="dinosaurs">
<complexType>
<sequence>
<element name="source" type="normalizedString"/>
<element name="period" default="Cretaceous">
<simpleType>
<restriction base="string">
<enumeration value="Triassic" />
<enumeration value="Jurassic" />
<enumeration value="Cretaceous" />
</restriction>
</simpleType>
</element>

<element name="version" type="decimal"
fixed="1.0"/>
</sequence>
</complexType>
</element>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 7 '07 #4
Martin - YES! had to create a sequence before the Visual Studio XSD editor
would allow me to change the attribute into an element.

The XML produced from the class is just about perfect, except for one thing.

How can I get the class produced by the XSD to create XML that has:

xsi:schemaLocation="http://www.example.com/dinosaurs dinosaurs.xsd" specified at
the top?

Should this be specified in the XSD itself, or added to the C# class created in
some fashion?

Thanks, Les Caudle
On Mon, 07 May 2007 16:10:22 +0200, Martin Honnen <ma*******@yahoo.dewrote:
>Les Caudle wrote:
>My goal is to create an XML file from that class that would look like this:

<?xml version="1.0" encoding="utf-8"?>
<dinosaurs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/dinosaurs">
<source>dog</source>
<period>Cretaceous</period>
<version>1.0<version>
</dinosaurs>

So source, period and version are elements, not attributes.

><schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:dino="http://example.com/test" targetNamespace="http://example.com/test"
elementFormDefault="qualified">

The namespace above is http://www.example.com/dinosaurs not
http://example.com/test so first thing to change is the targetNamespace
of the schema to e.g.
targetNamespac="http://www.example.com/dinosaurs"
> <element name="dinosaurs">
<complexType>
<attribute name="version" type="decimal" fixed="1.0" />
<attribute name="source" type="normalizedString" />
<attribute name="period" default="Cretaceous">
<simpleType>
<restriction base="string">
<enumeration value="Triassic" />
<enumeration value="Jurassic" />
<enumeration value="Cretaceous" />
</restriction>
</simpleType>
</attribute>
</complexType>

Then inside of the complexType define a sequence e.g.

<element name="dinosaurs">
<complexType>
<sequence>
<element name="source" type="normalizedString"/>
<element name="period" default="Cretaceous">
<simpleType>
<restriction base="string">
<enumeration value="Triassic" />
<enumeration value="Jurassic" />
<enumeration value="Cretaceous" />
</restriction>
</simpleType>
</element>

<element name="version" type="decimal"
fixed="1.0"/>
</sequence>
</complexType>
</element>
May 7 '07 #5
Hi Les,
Thanks for Martin's suggestion.

In order to add schemaLocation specified at the top of xml file, you could
add the following property into the class.

[XmlAttributeAttribute("schemaLocation",Namespace=" http://www.w3.org/2001/XM
LSchema-instance")]
public string xsiSchemaLocation = "http://www.example.com/dinosaurs
dinosaurs.xsd";

Hope this helps. Please let me know If you have any more concern on this.
Sincerley,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 8 '07 #6
Hi Les,

Have you resolved the issue so far?
Please let me know if you meet any further issue on this or have anything
unclear.
I'm glad to assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 10 '07 #7
Wen - that does resolve the issue.

I'd like to be able to specify it somehow in the XSD or XSD editor within Visual
Studio, but I can certainly copy that by hand into the class if that is the only
way to go

Thanks, Les Caudle

On Thu, 10 May 2007 10:35:12 GMT, v-******@online.microsoft.com (WenYuan Wang
[MSFT]) wrote:
>Hi Les,

Have you resolved the issue so far?
Please let me know if you meet any further issue on this or have anything
unclear.
I'm glad to assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
================================================= =
This posting is provided "AS IS" with no warranties, and confers no rights.
May 10 '07 #8
Hi Les,

As far as I know, it cannot be specified in XSD file. You may copy that in
class manually .

Have a great day. Please let me know if you have anything unclear. I'm glad
to assist you.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 14 '07 #9

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

Similar topics

2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
0
by: bbalet.free.fr | last post by:
The “Add Web Reference” Visual tool generates bad classes (from WSDL schema) for ComplexType containing only one element (wsdl.exe and wseWsdl3.exe tools have the same problem) : if a...
4
by: joe | last post by:
I have the following header file generated by Java JNI's header file generator (see bottom). Obviously I will write a source file which will provide an implementation of these methods. I have...
4
by: Fabio | last post by:
An ASP.NET 2.0 web site contains a web form and a web service. The web form consumes the web service. There is a Book class in the App_Code folder. The web service exposes a method that returns a...
1
by: robc | last post by:
Hi, I am using the xsd.exe tool (in .NET 2.0) to generate C# classes from a set of schemas. Some of the XML types that I have defined are used by more than one schema. For maintenance...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
32
by: Immortal Nephi | last post by:
I want to know if the practice is the best. Do I need to place inline keyword inside class definition or outside member function definition. For example class A { public: A(); ~A();
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.