如何在现有XML中添加Child元素

时间:2012-01-27 12:29:47

标签: c# xml

我有一个问题我想在我的现有Xml中添加子元素我该怎么做 请帮助我enter image description here

3 个答案:

答案 0 :(得分:2)

简单:

  • 将XML加载到内存
  • 找到您要追加的现有节点
  • 创建新元素
  • 拨打XNode.AddAfterSelf
  • 之类的内容
  • 保存结果

确切的调用取决于您使用的库;我个人建议你尽可能使用LINQ to XML(例如,如果你使用的是.NET 3.5或更高版本),因为它比早期的API更容易使用。

答案 1 :(得分:1)

您可以使用the XDocument class在C#中轻松操作Xml:

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

答案 2 :(得分:1)

我正在使用LINQ-to-XML,这对我来说似乎更容易,我就是这样做的

首先加载

        /// <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;
        }

在另一个方法中调用上述方法

        /// <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
        }

用于子元素创建

        /// <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;
        }

CreateRoomIncomingMemberXElement方法中,您将根据自己的要求创建自己的子元素,然后在AddMemberNodeToRoomMembersXMLWithGivenModelHesapAdiAndUyeHesapAdi中调用它并将其添加到已加载的文件中,然后保存。