JMon
JMon

Reputation: 3447

Adding multiple child nodes to the Parent node

I am trying to create and XML file with data I am collecting, however for a particular I need multiple sub tags to be generated

So I wish to have something like this:-

<Feedbacks>
   <Feedback>
     <Name></Name>
     <Surname></Surname>
     <Images>
       <Image></Image>
       <Image></Image>
       <Image></Image>
     </Images>
   </Feedback>
</Feedbacks>

Sorry do not know how to paste the correct XML file here, but I think you get the idea. At the moment I have this code that is working:-

    private static void CreateFeedbackXMLFile()
    {
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        XmlNode Node = doc.CreateElement("Feedbacks");
        doc.AppendChild(Node);

        string fileName = "Feedback.xml";
        string filePath = Properties.Settings.Default.DefaultFolder + "\\" + fileName;

        doc.Save(filePath);
    }

    public static void InsertFeedback(Feedback feedback)
    {
        CreateFeedbackXMLFile();

        string filePath = Properties.Settings.Default.DefaultFolder + "\\Feedback.xml" ;
        XDocument xmlDoc = XDocument.Load(filePath);
        XElement XParentElement = new XElement("Feedback");

        InsertIntoXMLDoc(feedback, filePath, xmlDoc);
    }


    private static void InsertIntoXMLDoc(Feedback feedback, string filePath, XDocument xmlDoc)
    {

        xmlDoc.Element("Feedbacks").Add(new XElement("Feedback",
                                        new XElement("Name", feedback.Name),
                                        new XElement("Surname", feedback.Surname),
                                        new XElement("Email", feedback.Email),
                                        new XElement("Website", feedback.Website),
                                        new XElement("Suggestion", feedback.Suggestion),
                                        new XElement("Error", feedback.Error),
                                        new XElement("MailingList", feedback.MailingList),
                                        new XElement("Comments", feedback.Comments)
                            ));

        }
        xmlDoc.Save(filePath);
    }

Now I need to loop through the imageList and create nodes according to how many images I have.

Thanks for your help and time

Upvotes: 2

Views: 21440

Answers (1)

Edurne Pascual
Edurne Pascual

Reputation: 5667

Looping is the way to go for what you are trying. In fact, there is no "loopless" way to achieve that. You can, however, disguise the loop as a LINQ query, with something like this:

xmlDoc.Element("Feedbacks").Add(
    /* All the elements before your image list */
    XElement("images", 
        from img in myImageList select new XElement(...)
    )
    /* All the elements after your image list, preceeded by a comma */
);

Of course, you'll need to replace myImageList with your actual collection of images. Note that if you have an ImageList control, the actual collection is not the control itself, but its Images property.

Also, on the ..., you'll need to put whatever logics you are using to create each node from an image (using the auto-typed local variable img to refer to the appropriate image for each node).

Upvotes: 2

Related Questions