SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)

    Question XML Serialization - Need to match third party content

    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
    Code:
    		<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:
    Code:
    	[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:
    Code:
        <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
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I figured it out immediately after posting this.

    Code:
    	[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;
    		[XmlElement(ElementName = "Bin")]
    		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;
    	}


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •