Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization of array of xml elements already within a xmlAny Element

<Fruits>

            <FruitName>Amla</FruitName> 
            <FruitPrice>10 US DOLLARS</FruitPrice> 

            <Origin>
                <Country>INDIA</Country> 
                <NativeName>GOOSEBERRY</NativeName> 
                <Availability>PLENTY</Availability> 
            </Origin>

            <OtherInfo>
                <FiberPercentage>1.11</FiberPercentage> 
                <MagnesiumPercentage>0.02</MagnesiumPercentage> 
            </OtherInfo>

While De serializing the above XML structure, I use something like,

Xml

XmlElement("FruitsList")]
public List<Fruits> FruitsImport { get; set; }

In Fruits Class, I have something like:

    [XmlAnyElement]
    public List<XmlElement> FruitElements { get; set; }

    [XmlElement("Origin")]
    public List<XmlElement> FruitOrigin { get; set; }

    [XmlElement("OtherInfo")]
    public List<XmlElement> OtherInfo { get; set; }

FruitElement retrieves the FruitName and FruitPrice. FruitOrigin retrives Country Info alone. OtherInfo retrives FiberPercentage alone.

Any ideas on how to get all the info under <Origin> and <OtherInfo> tags ?

like image 559
now he who must not be named. Avatar asked Dec 03 '25 18:12

now he who must not be named.


1 Answers

Because you have elements nested in both <origin> and <otherinfo> tags, you need to define a class for them as well when performing deserialization.

[XmlElement("Origin")]
public List<Origins> FruitOrigin { get; set; }

You would define the origin class the same way as you did for fruit class.

(The skeleton of Origin class would be something as below:

[Serializable]
public class Origin
{
    [XmlAnyElement]
    public List<XmlElement> OriginElements { get; set; }
}

)

like image 88
l46kok Avatar answered Dec 06 '25 07:12

l46kok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!