Perhaps I'm using this wrong. When I use
[System.Xml.Serialization.XmlIgnoreAttribute()] 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();
InitializeComponent();
try
{
XmlSerializer x = new XmlSerializer(typeof(testClass));
TextWriter writer = new StreamWriter("\\" + "test.xml");
x.Serialize(writer, 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.Serialization;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Xml.Schema;
using System.IO;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
// [System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true)]
public partial class testClass {
private string eventTypeField;
private string pSCommonField;
private string loadArrivalNumberField;
private string incomingLDSNumberField;
private string transportIdentifierField;
private string sourceField;
private string cutblockIDField;
private string fieldScaleFlagField;
private string fieldScaleDeckIDField;
private string weighSlipNumberField;
private string originatingScaleSiteNumberField;
private string splitLoadField;
private string beachcombField;
private string checkScaleField;
private string pSMeasureField;
private string dDNField;
private string dvField;
private bool beachcombFieldSpecified;
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public string EventType
{
get
{ return this.eventTypeField;}
set
{this.eventTypeField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public string PSCommon
{
get
{ return this.pSCommonField;}
set
{ this.pSCommonField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 2)]
public string LoadArrivalNumber
{
get
{return this.loadArrivalNumberField; }
set
{this.loadArrivalNumberField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Order = 3)]
public string IncomingLDSNumber
{
get
{ return this.incomingLDSNumberField;}
set
{this.incomingLDSNumberField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 4)]
public string TransportIdentifier
{
get
{ return this.transportIdentifierField; }
set
{ this.transportIdentifierField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 5)]
public string Source
{
get
{ return this.sourceField; }
set
{ this.sourceField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 6)]
public string CutblockID
{
get
{return this.cutblockIDField;}
set
{this.cutblockIDField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Order = 7)]
public string FieldScaleFlag
{
get
{ return this.fieldScaleFlagField; }
set
{ this.fieldScaleFlagField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Order = 8)]
public string FieldScaleDeckID
{
get
{ return this.fieldScaleDeckIDField; }
set
{ this.fieldScaleDeckIDField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 9)]
public string WeighSlipNumber
{
get
{ return this.weighSlipNumberField; }
set
{ this.weighSlipNumberField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Order = 10)]
public string OriginatingScaleSiteNumber
{
get
{ return this.originatingScaleSiteNumberField; }
set
{ this.originatingScaleSiteNumberField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Order = 11)]
public string SplitLoad
{
get
{ return this.splitLoadField;}
set
{ this.splitLoadField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 12)]
public string Beachcomb
{
get
{ return this.beachcombField;}
set
{ this.beachcombField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 13)]
public string CheckScale
{
get
{ return this.checkScaleField;}
set
{this.checkScaleField = value;}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 14)]
public string PSMeasure
{
get
{ return this.pSMeasureField; }
set
{ this.pSMeasureField = value; }
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DDN
{
get
{ return this.dDNField; }
set
{ this.dDNField = value; }
}
[System.Xml.Serialization.XmlAttributeAttribute(Dat aType = "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.Serialization.XmlAttributeAttribute()
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool BeachcombSpecified
{
get
{ return this.beachcombFieldSpecified; }
set
{ this.beachcombFieldSpecified = value; }
}
public testClass()
{
EventType = "1";
PSCommon = "2";
LoadArrivalNumber = "3";
IncomingLDSNumber = "4";
TransportIdentifier = "5";
Source = "6";
CutblockID = "7";
FieldScaleFlag = "8";
FieldScaleDeckID = "9";
WeighSlipNumber = "10";
OriginatingScaleSiteNumber = "11";
SplitLoad = "12";
Beachcomb = "13";
CheckScale = "14";
PSMeasure = "15";
DDN = "16";
DV = "17";
BeachcombSpecified = 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>
<LoadArrivalNumber>3</LoadArrivalNumber>
<IncomingLDSNumber>4</IncomingLDSNumber>
<TransportIdentifier>5</TransportIdentifier>
<Source>6</Source>
<CutblockID>7</CutblockID>
<FieldScaleFlag>8</FieldScaleFlag>
<FieldScaleDeckID>9</FieldScaleDeckID>
<WeighSlipNumber>10</WeighSlipNumber>
<OriginatingScaleSiteNumber>11</OriginatingScaleSiteNumber>
<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.Serialization.XmlIgnoreAttribute()]
public bool BeachcombSpecified
{
get
{ return this.beachcombFieldSpecified; }
set
{ this.beachcombFieldSpecified = value; }
}
and it's entry in the constructor
BeachcombSpecified = false; //should be ignored
they do make it
Once again this is done in the compact framework