// : xsd [System.SerializableAttribute] [XmlTypeAttribute(AnonymousType = true)] public class DtoProduct { private string productidfield; private string productnamefield; private string productpricefield; /*...*/ [XmlElement] public string productid { get{return this.productidfield;} set{this.productidfield = value;} } [XmlElement] public string productname { get{return this.productnamefield;} set{this.productnamefield = value;} } [XmlElement] public string productprice { get{return this.productpricefield;} set{this.productpricefield = value;} } }
[System.SerializableAttribute] [XmlTypeAttribute(AnonymousType = true)] public class DtoProduct { private decimal _productPrice; //... [XmlElement("productprice")] public decimal ProductPrice { get{return _productPrice;} set{_productPrice = value;} } }
public class XmlMoneyWrapper : IXmlSerializable { public decimal Value { get; set; } // public override string ToString() { return Value.ToString("0.00", CultureInfo.InvariantCulture); } #region IXmlSerializable Members public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { string value = reader.ReadString(); // TODO change to TryParse? try { Value = Decimal.Parse(value, new NumberFormatInfo { NumberDecimalSeparator = "." }); } catch (Exception exc) { String err = String.Format("Can't deserialize string {0} to decimal. Expected number decimal separator is dot \".\"", value); throw new SerializationException(err, exc); } reader.Read(); } public void WriteXml(XmlWriter writer) { writer.WriteString(ToString()); } #endregion }
[System.SerializableAttribute] [XmlTypeAttribute(AnonymousType = true)] public class DtoProduct { private XmlMoneyWrapper _productPrice; //... [XmlElement("productprice")] public XmlMoneyWrapper ProductPrice // , { get { return _productPrice; } set { _productPrice = value; } } }
public static implicit operator XmlMoneyWrapper(decimal arg) // decimal to XmlMoneyWrapper { XmlMoneyWrapper res = new XmlMoneyWrapper { Value = arg }; return res; }
public static implicit operator decimal (XmlMoneyWrapper arg) // XmlMoneyWrapper to decimal { return arg.Value; }
public static implicit operator XmlLimitedStringWrapper(string arg)
[System.SerializableAttribute] [XmlTypeAttribute(AnonymousType = true)] public class DtoProduct { private readonly XmlLimitedStringWrapper _productName = new XmlLimitedStringWrapper(16); // Create string with maxLength = 16. // ... // Max symbols: 16. [XmlElement("productname")] public string ProductName { get{return _productName = _productName.Value;} set{_productName.Value = value;} } }
Source: https://habr.com/ru/post/270473/
All Articles