HighTechTalks DotNet Forums  

Formatting XmlSerializer Output

Dotnet XML microsoft.public.dotnet.xml


Discuss Formatting XmlSerializer Output in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
jasonterando@yahoo.com
 
Posts: n/a

Default Formatting XmlSerializer Output - 12-14-2007 , 03:11 PM






I'm using classes generated from xsd.exe. I am serializing them with
XmlTextWriter, and everything works fine. Our customer, though, has a
silly requirement that dollar amounts (in the .xsd as decimals) be
formatted to always include cents (regardless if there are cents or
not). I'm interested in using the classes to only output data (i.e.
I'm not parsing incoming data)

I've tried setting up the following construct to format the output of
each of the affected decimal fields, but I don't get anything written
out to the .xml file. What am I missing - and is there a better way?

/// Override for Amount output
[System.Xml.Serialization.XmlAttributeAttribute(Att ributeName =
"Amount", DataType="string")]
public string AmountString {
get {
return string.Format("#0.00", this.amountField);
}
}

[System.Xml.Serialization.XmlIgnore()]
public decimal Amount {
get {
return this.amountField;
}
set {
this.amountField = value;
}

Reply With Quote
  #2  
Old   
Joe Fawcett
 
Posts: n/a

Default Re: Formatting XmlSerializer Output - 12-16-2007 , 06:18 AM






<jasonterando (AT) yahoo (DOT) com> wrote

Quote:
I'm using classes generated from xsd.exe. I am serializing them with
XmlTextWriter, and everything works fine. Our customer, though, has a
silly requirement that dollar amounts (in the .xsd as decimals) be
formatted to always include cents (regardless if there are cents or
not). I'm interested in using the classes to only output data (i.e.
I'm not parsing incoming data)

I've tried setting up the following construct to format the output of
each of the affected decimal fields, but I don't get anything written
out to the .xml file. What am I missing - and is there a better way?

/// Override for Amount output
[System.Xml.Serialization.XmlAttributeAttribute(Att ributeName =
"Amount", DataType="string")]
public string AmountString {
get {
return string.Format("#0.00", this.amountField);
}
}

[System.Xml.Serialization.XmlIgnore()]
public decimal Amount {
get {
return this.amountField;
}
set {
this.amountField = value;
}
Xml serialisation does not serialise read only properties. You can put in a
set method for AmountString and get it to throw an error if used. Not ideal,
I know.
The only other option I can think of is to implement IXmlSerializable and
take control of the whole process.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.