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?
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?