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,
}
===========================================