Shashank
Shashank

Reputation: 6337

how to add Child element in Existing XML

i have a Question i want to add Child Element in My Existing Xml How Can i Do this please help meenter image description here

Upvotes: 1

Views: 7523

Answers (3)

Bastardo
Bastardo

Reputation: 4152

I'm using LINQ-to-XML, this seems easier to me and that is how I do it

First load it

        /// <summary>
        /// loads and returns the XML file with the given name
        /// </summary>
        /// <param name="modelHesapAdi"> name of the XML file to be returned</param>
        /// <returns>returns the xml of given model hesap adı</returns>
        public static XElement LoadXMLWithGivenModelHesapAdi(string modelHesapAdi, string xmlDirectory)
        {
            XElement modelsXmlFile = XElement.Load(xmlDirectory + modelHesapAdi + ".xml");

            return modelsXmlFile;
        }

Call the above method in another one

        /// <summary>
        /// gets a roommessage nood from CreateRoomMessageXElement
        /// and adds it to the related room XML file and saves it
        /// </summary>
        /// <param name="modelHesapAdi">a string which has the name of the XML file to be changed</param>
        /// <param name="incomingMemberHesapAdi">a string to be inserted to the xml file, which has the members name</param>
        /// <param name="entranceTime"> a string for time, holds the member's entrance time</param>
        public void AddMemberNodeToRoomMembersXMLWithGivenModelHesapAdiAndUyeHesapAdi(string modelHesapAdi, 
                                                                                      string incomingMemberHesapAdi,
                                                                                      string entranceTime)
        {
            XElement modelsXmlFile = BAL.Models.Model.LoadXMLWithGivenModelHesapAdi(modelHesapAdi, xmlDirectory);//loads the xml
            XElement roomMember = CreateRoomIncomingMemberXElement(incomingMemberHesapAdi, entranceTime);//creates child element and returns it
            modelsXmlFile.Add(roomMember);//adds the child element
            modelsXmlFile.Save(xmlDirectory + modelHesapAdi + ".xml");//saves the edited file
        }

For child element creation

        /// <summary>
        /// creates and returns roommessage nood
        /// </summary>
        /// <param name="memberHesapAdi">the sender of the message</param>
        /// <param name="message">sent message</param>
        /// <param name="timeSent">the time when the message was sent</param>
        /// <returns></returns>
        private XElement CreateRoomIncomingMemberXElement(string memberHesapAdi, string entranceTime)
        {
            XElement roomMessage = new XElement("RoomMember",
                                                            new XElement("MemberHesapAdi", memberHesapAdi),
                                                            new XElement("Time", entranceTime));
            return roomMessage;
        }

In CreateRoomIncomingMemberXElement method you will create your own child element with your requirements, you will call it in AddMemberNodeToRoomMembersXMLWithGivenModelHesapAdiAndUyeHesapAdi and add it to the loaded file, then save it.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499800

Simple:

  • Load the XML into memory
  • Find the existing node where you want to append
  • Create the new element
  • Call something like XNode.AddAfterSelf
  • Save the result

The exact calls will depend on which library you use; personally I'd suggest using LINQ to XML if you possibly can (i.e. if you're using .NET 3.5 or higher) as it's much easier to use than the earlier APIs.

Upvotes: 2

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

You can use the XDocument class to easily manipulate Xml in C#:

var doc = XDocument.Parse(yourXmlString); // Or XDocument.Load(pathToFile);
var childElement = new XElement("YourChildElementName", yourChildElementValue);
doc.Add(childElement);

Upvotes: 1

Related Questions