Perhaps I'm using this wrong. When I use
[System.Xml.Seri alization.XmlIg noreAttribute()] above an attribute I don't
want serialized it ignores the attribute. It also ignores some other
attributes. Have I done something wrong?
Here's the serialization code
public Form1()
{
testClass test = new testClass();
InitializeCompo nent();
try
{
XmlSerializer x = new XmlSerializer(t ypeof(testClass ));
TextWriter writer = new StreamWriter("\ \" + "test.xml") ;
x.Serialize(wri ter, test);
}
catch (Exception e)
{
MessageBox.Show (e.ToString());
}
}
Here's the class being serialized. Sorry that it's so long but I couldn't
reproduce this with a shorter class I made
namespace XML_test1{
using System.Xml.Seri alization;
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Xml;
using System.Xml.Sche ma;
using System.IO;
[System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
// [System.Serializ ableAttribute()]
[System.Diagnost ics.DebuggerSte pThroughAttribu te()]
[System.Componen tModel.Designer CategoryAttribu te("code")]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true )]
public partial class testClass {
private string eventTypeField;
private string pSCommonField;
private string loadArrivalNumb erField;
private string incomingLDSNumb erField;
private string transportIdenti fierField;
private string sourceField;
private string cutblockIDField ;
private string fieldScaleFlagF ield;
private string fieldScaleDeckI DField;
private string weighSlipNumber Field;
private string originatingScal eSiteNumberFiel d;
private string splitLoadField;
private string beachcombField;
private string checkScaleField ;
private string pSMeasureField;
private string dDNField;
private string dvField;
private bool beachcombFieldS pecified;
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 0)]
public string EventType
{
get
{ return this.eventTypeF ield;}
set
{this.eventType Field = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 1)]
public string PSCommon
{
get
{ return this.pSCommonFi eld;}
set
{ this.pSCommonFi eld = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 2)]
public string LoadArrivalNumb er
{
get
{return this.loadArriva lNumberField; }
set
{this.loadArriv alNumberField = value; }
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 3)]
public string IncomingLDSNumb er
{
get
{ return this.incomingLD SNumberField;}
set
{this.incomingL DSNumberField = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 4)]
public string TransportIdenti fier
{
get
{ return this.transportI dentifierField; }
set
{ this.transportI dentifierField = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 5)]
public string Source
{
get
{ return this.sourceFiel d; }
set
{ this.sourceFiel d = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 6)]
public string CutblockID
{
get
{return this.cutblockID Field;}
set
{this.cutblockI DField = value; }
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 7)]
public string FieldScaleFlag
{
get
{ return this.fieldScale FlagField; }
set
{ this.fieldScale FlagField = value; }
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 8)]
public string FieldScaleDeckI D
{
get
{ return this.fieldScale DeckIDField; }
set
{ this.fieldScale DeckIDField = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 9)]
public string WeighSlipNumber
{
get
{ return this.weighSlipN umberField; }
set
{ this.weighSlipN umberField = value; }
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 10)]
public string OriginatingScal eSiteNumber
{
get
{ return this.originatin gScaleSiteNumbe rField; }
set
{ this.originatin gScaleSiteNumbe rField = value; }
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 11)]
public string SplitLoad
{
get
{ return this.splitLoadF ield;}
set
{ this.splitLoadF ield = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 12)]
public string Beachcomb
{
get
{ return this.beachcombF ield;}
set
{ this.beachcombF ield = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 13)]
public string CheckScale
{
get
{ return this.checkScale Field;}
set
{this.checkScal eField = value;}
}
[System.Xml.Seri alization.XmlEl ementAttribute( Order = 14)]
public string PSMeasure
{
get
{ return this.pSMeasureF ield; }
set
{ this.pSMeasureF ield = value; }
}
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public string DDN
{
get
{ return this.dDNField; }
set
{ this.dDNField = value; }
}
[System.Xml.Seri alization.XmlAt tributeAttribut e(DataType = "integer")]
public string DV
{
get
{ return this.dvField; }
set
{ this.dvField = value; }
}
// Somehow this ignore statement is removing other attributes not just this
attribute
// System.Xml.Seri alization.XmlAt tributeAttribut e()
[System.Xml.Seri alization.XmlIg noreAttribute()]
public bool BeachcombSpecif ied
{
get
{ return this.beachcombF ieldSpecified; }
set
{ this.beachcombF ieldSpecified = value; }
}
public testClass()
{
EventType = "1";
PSCommon = "2";
LoadArrivalNumb er = "3";
IncomingLDSNumb er = "4";
TransportIdenti fier = "5";
Source = "6";
CutblockID = "7";
FieldScaleFlag = "8";
FieldScaleDeckI D = "9";
WeighSlipNumber = "10";
OriginatingScal eSiteNumber = "11";
SplitLoad = "12";
Beachcomb = "13";
CheckScale = "14";
PSMeasure = "15";
DDN = "16";
DV = "17";
BeachcombSpecif ied = false; //should be ignored
}
}
}
Here's the results
<?xml version="1.0" encoding="utf-8"?>
<testClass xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
<EventType>1</EventType>
<PSCommon>2</PSCommon>
<LoadArrivalNum ber>3</LoadArrivalNumb er>
<IncomingLDSNum ber>4</IncomingLDSNumb er>
<TransportIdent ifier>5</TransportIdenti fier>
<Source>6</Source>
<CutblockID>7 </CutblockID>
<FieldScaleFlag >8</FieldScaleFlag>
<FieldScaleDeck ID>9</FieldScaleDeckI D>
<WeighSlipNumbe r>10</WeighSlipNumber >
<OriginatingSca leSiteNumber>11 </OriginatingScal eSiteNumber>
<SplitLoad>12 </SplitLoad>
</testClass>
as you can see the last 5 fields did not make it to the XML.
If you simply comment out the
[System.Xml.Seri alization.XmlIg noreAttribute()]
public bool BeachcombSpecif ied
{
get
{ return this.beachcombF ieldSpecified; }
set
{ this.beachcombF ieldSpecified = value; }
}
and it's entry in the constructor
BeachcombSpecif ied = false; //should be ignored
they do make it
Once again this is done in the compact framework