I am decent with defining objects to match an XML specification, however, I haven’t been able to work my way around the following
Third Party snippet
<DerivedVariable Name="AGENTAGEBin">
<BinSort UseVariable="AGENTAGE">
<Bin UpperBound="-1" Value="3" />
<Bin LowerBound="0" UpperBound="0" Value="0" />
<Bin LowerBound="1" UpperBound="3" Value="1" />
<Bin LowerBound="4" UpperBound="6" Value="2" />
<Bin LowerBound="7" UpperBound="11" Value="3" />
<Bin LowerBound="12" UpperBound="20" Value="4" />
<Bin LowerBound="21" UpperBound="37" Value="5" />
<Bin LowerBound="38" Value="6" />
</BinSort>
</DerivedVariable>
My objects:
[Serializable]
[XmlRoot(ElementName = "DerivedVariable")]
public class DerivedVariable
{
[XmlAttribute(AttributeName = "Name")]
public string Name;
[XmlElement(ElementName = "Multiplier", IsNullable = true)]
public decimal? Multiplier;
[XmlElement(ElementName = "Calculation")]
public string Calculation;
[XmlElement(ElementName = "BinSort")]
public BinContainer BinSort;
public bool ShouldSerializeMultiplier()
{
return this.Multiplier.HasValue;
}
}
[Serializable]
[XmlRoot(ElementName = "BinSort")]
public class BinContainer
{
[XmlAttribute(AttributeName = "UseVariable")]
public string UseVariable;
[XmlArray(ElementName = "")]
public Bin[] Bins;
}
[Serializable]
[XmlRoot(ElementName = "")]
public class Bin
{
[XmlAttribute(AttributeName = "UpperBound")]
public string UpperBound;
[XmlAttribute(AttributeName = "LowerBound")]
public string LowerBound;
[XmlAttribute(AttributeName = "Value")]
public string Value;
}
My output:
<DerivedVariable Name="AGENTAGEBin">
<BinSort UseVariable="AGENTAGE">
<Bins>
<Bin UpperBound="-1" Value="3" />
<Bin UpperBound="0" LowerBound="0" Value="0" />
<Bin UpperBound="3" LowerBound="1" Value="1" />
<Bin UpperBound="6" LowerBound="4" Value="2" />
<Bin UpperBound="11" LowerBound="7" Value="3" />
<Bin UpperBound="20" LowerBound="12" Value="4" />
<Bin UpperBound="37" LowerBound="21" Value="5" />
<Bin LowerBound="38" Value="6" />
</Bins>
</BinSort>
</DerivedVariable>
Noticed that there is a <Bins> element that surrounds the <Bin> elements. I need to get rid of the <Bins> element. Also take note that I need to keep the attribute on the BinSort element.
My initial idea was to make BinSort the array of Bins, but then I couldn’t figure out how to get the attribute on that element.
Thanks,
Matt