XmlSerializer doesn't like XmlAttributes with numeric data type?

H

Hardy Wang

I have a very simple XSD file, an element with 3 attributes string, DateTime
and int:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:attribute name="AttrA" type="xs:string" />
<xs:attribute name="AttrB" type="xs:dateTime" />
<xs:attribute name="AttrC" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:schema>

I ran "xsd test.asd /classes" command to generate classes.
using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[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 Root {

private string attrAField;

private System.DateTime attrBField;

private bool attrBFieldSpecified;

private int attrCField;

private bool attrCFieldSpecified;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string AttrA {
get {
return this.attrAField;
}
set {
this.attrAField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime AttrB {
get {
return this.attrBField;
}
set {
this.attrBField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AttrBSpecified {
get {
return this.attrBFieldSpecified;
}
set {
this.attrBFieldSpecified = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public int AttrC {
get {
return this.attrCField;
}
set {
this.attrCField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AttrCSpecified {
get {
return this.attrCFieldSpecified;
}
set {
this.attrCFieldSpecified = value;
}
}
}

Then I have following code to populate objects and serialize it:
Root r = new Root();
r.AttrA = "aaa";
r.AttrB = DateTime.Now;
r.AttrC = 100;

string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(Root));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, r);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());

After I get the serialized Xml string, I found only AttrA, which is string
type, is serialized, program just eats AttrB and AttrC.

Do I miss something? How can I let program to serialize all attributes?
 
M

Martin Honnen

Hardy said:
I have a very simple XSD file, an element with 3 attributes string, DateTime
and int:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:attribute name="AttrA" type="xs:string" />
<xs:attribute name="AttrB" type="xs:dateTime" />
<xs:attribute name="AttrC" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:schema>

I ran "xsd test.asd /classes" command to generate classes.
using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[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 Root {

private string attrAField;

private System.DateTime attrBField;

private bool attrBFieldSpecified;

private int attrCField;

private bool attrCFieldSpecified;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string AttrA {
get {
return this.attrAField;
}
set {
this.attrAField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime AttrB {
get {
return this.attrBField;
}
set {
this.attrBField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AttrBSpecified {

This property
get {
return this.attrBFieldSpecified;
}
set {
this.attrBFieldSpecified = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public int AttrC {
get {
return this.attrCField;
}
set {
this.attrCField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AttrCSpecified {

and this property are generated as your attribute declarations don't
have use="required".

Fix your schema to

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:attribute name="AttrA" type="xs:string" />
<xs:attribute name="AttrB" type="xs:dateTime" use="required"/>
<xs:attribute name="AttrC" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

then you get the behaviour you want.
 
H

Hardy Wang

I later on used Microsoft Sample Code Generator at
http://www.microsoft.com/downloads/...E5-F66C-4A4D-933B-46222BB01EB0&displaylang=en to create all classes and it works for me.

It seems that built in XSD shipped with VS2005 is not working so well.
--
Regards
Hardy


Martin Honnen said:
Hardy said:
I have a very simple XSD file, an element with 3 attributes string, DateTime
and int:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:attribute name="AttrA" type="xs:string" />
<xs:attribute name="AttrB" type="xs:dateTime" />
<xs:attribute name="AttrC" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:schema>

I ran "xsd test.asd /classes" command to generate classes.
using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[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 Root {

private string attrAField;

private System.DateTime attrBField;

private bool attrBFieldSpecified;

private int attrCField;

private bool attrCFieldSpecified;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string AttrA {
get {
return this.attrAField;
}
set {
this.attrAField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime AttrB {
get {
return this.attrBField;
}
set {
this.attrBField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AttrBSpecified {

This property
get {
return this.attrBFieldSpecified;
}
set {
this.attrBFieldSpecified = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public int AttrC {
get {
return this.attrCField;
}
set {
this.attrCField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AttrCSpecified {

and this property are generated as your attribute declarations don't
have use="required".

Fix your schema to

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:attribute name="AttrA" type="xs:string" />
<xs:attribute name="AttrB" type="xs:dateTime" use="required"/>
<xs:attribute name="AttrC" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

then you get the behaviour you want.
 

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

Top