user2219930
user2219930

Reputation: 125

Add an XML wrapper element to serialized data

I have a couple of lists that I have serialized into an XML file.

I am having difficulty figuring out how to put an XML wrapper element around my element list.

Currently my XML looks like this:

    <finding>
        <issue1></issue1>
        <issue2><issue2 />
        <issue3><issue3 />
    </finding>
<finding>
        <issue1></issue1>
        <issue2><issue2 />
        <issue3><issue3 />
    </finding>

I want to wrap these finding elements so the XML looks like this instead:

   <Findings>       
        <finding>
                <issue1></issue1>
                <issue2><issue2 />
                <issue3><issue3 />
            </finding>
        <finding>
                <issue1></issue1>
                <issue2><issue2 />
                <issue3><issue3 />
            </finding>
    <findings/>

Here is how I am serializing:

[XmlElement("finding")]
        public List<Findings> Findings { get {return findings;}}


                    [XmlElement("Issue1")]
                    public string getIssue1 { get { return issue1; } }

                    [XmlElement("issue2")]
                    public string getissue2 { get { return issue2; } }

                    [XmlElement("issue3")]
                    public string getissue3 { get { return issue3; } }

Here is the calling code:

        XmlSerializer ser = new XmlSerializer(typeof(RuleSet));
        using (StreamWriter writer = new StreamWriter(fileName))
        {

            ser.Serialize(writer, findings);                    
        }  

Findings Class:

 [Serializable()]
    [XmlRoot("Findings")]   
    public class Findings
    {         

        public string issue1;
        public string issue2;
        public string issue3;
        public string issue4;

        public Findings()
        { 

        }
        public Findings(string string flag1, string flag2, string flag3, string flag4)
        {
            this.issue1 = flag1;
            this.issue2 = flag2;
            this.issue3 = flag3;
            this.issue4 = flag4;


        }

    }

}

and the ruleset class

    [Serializable()]
    public class RuleSet
    {

        private List<Findings> findings  = new List<Findings>();

        Findings findresults = new Findings();   


        [XmlElement("finding")]
        public List<Findings> Findings { get {return findings;}}

                    [XmlElement("Issue1")]
                    public string getIssue1 { get { return findresults.issue1; } }

                    [XmlElement("issue2")]
                    public string getissue2 { get { return findresults.issue2; } }

                    [XmlElement("issue3")]
                    public string getissue3 { get { return findresults.issue3; } }

                    [XmlElement("issue4")]
                    public string getissue4 { get { return findresults.issue4; } }



        public void setFindings(List<Findings> findings)
        {
            this.findings = findings;
        }


        public void addFindings(Findings findings)
        {
            this.findings.Add(findings);
        }
    }

}

I have been fighting with this for hours. What am I missing?

Upvotes: 1

Views: 1270

Answers (1)

MikeH
MikeH

Reputation: 4395

This should work:

public class Findings
{
  [System.Xml.Serialization.XmlElementAttribute("finding")]
  public List<Finding> finding = new List<Finding>();
}
public class Finding
{
  public string issue1;
  public string issue2;
  public string issue3;
}

Note the XmlElementAttribute which names the items of the list. In this case a lower case finding. Also, my example uses fields for issue1 etc., but your properties will be just fine.

Upvotes: 1

Related Questions