Changing Name Of XML When Deserialising
I have the following code snippet generated by the xsd tool:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(开发者_运维问答"code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class myCourseCourseStructureModule
I would like to call the class something a bit more meaningful. What attribute do I need to add to allow me to change this easily?
XmlTypeAttribute has a constructor that takes a string which should be the name: XmlTypeAttribute Constructor on MSDN. As described in this related post, this will change the name of the complex type in the schema. John Saunders suggests using
[XmlElement(Name="MyAddress", Namespace="your namespace")]
to change the element name in the XML.
XmlElementAttribute
// This is the class that will be serialized.
public class XClass
{
/* The XML element name will be XName
instead of the default ClassName. */
[XmlElement(ElementName = "XName")]
public string ClassName;
}
精彩评论