As far as I know, it's not possible to use the XML with the "Producing_Unit" tag but ditch the matching Producing_Unit class with the standard attributes short of implementing the IXmlSerializable interface. Your best bet would be to separate your application/business logic from your serialization layer.
Keep your serialization data model simple and matching your XML schema (this means including the wrapping Producing_Unit class), then simply convert to/from that data model and a cleaner data model (without Producing_Unit) for the rest of your application to work with.
EDIT: Here's an implementation using the IXmlSerializable interface. I just whipped it off and honestly, don't know if it will work for all cases.
public class Unit
{
public string Name { get; set; }
public string ID { get; set; }
}
public class Produced_by : IXmlSerializable
{
public Unit Unit { get; set; }
public void WriteXml (XmlWriter writer)
{
writer.WriteStartElement("Produced_by");
writer.WriteStartElement("Producing_Unit");
writer.WriteStartElement("Unit");
writer.WriteAttributeString("ID", this.Unit.ID);
writer.WriteAttributeString("Name", this.Unit.Name);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
public void ReadXml (XmlReader reader)
{
while (reader.Read())
{
if (reader.Name == "Unit")
{
this.Unit = new Unit()
{
Name = reader.GetAttribute("Name"),
ID = reader.GetAttribute("ID")
};
break;
}
}
}
public XmlSchema GetSchema()
{
return(null);
}
}
I suspect that I'm performing the reading in a poor fashion, but this works in my local test. I still recommend separating your application and serialization concerns though and avoiding having to write an implementation like this.
Producing_Unitclass with the standard attributes short of implementing theIXmlSerializableinterface. Your best bet would be to separate your application/business logic from your serialization layer. Keep your serialization simple and matching your XML schema, then simply convert to/from that data model and a cleaner data model (withoutProducing_Unit) for the rest of your application to work with. - Chris Sinclair