简体   繁体   中英

add element node to xml file c#

i am trying to add a new element to node which has attribute same with input. I tried, but i am beginning in xml. How can i do it ?

old xml file:

<Root>
 <Chatter Name="Dat" ID="1">
   <Content  Time="06/05/2014 2:00:08 PM">Send Content</Content>
   <Content  Time="06/05/2014 2:00:50 PM">Recieve Content</Content>
 </Chatter>
 <Chatter Name="Khang" ID="2">
    <Content  Time="06/05/2014 2:01:40 PM">Send Content</Content>
    <Content  Time="06/05/2014 2:02:00 PM">Recieve Content</Content>
 </Chatter>
 <Chatter Name="Khanh" ID="12">
    <Content Time="06/05/2014 2:03:10 PM">them moi</Content>
 </Chatter>
</Root>

i want to add a new element to node which has attribute Name="Khang" if it exist ,so here is new xml file

<Root>
 <Chatter Name="Dat" ID="1">
   <Content  Time="06/05/2014 2:00:08 PM">Send Content</Content>
   <Content  Time="06/05/2014 2:00:50 PM">Recieve Content</Content>
 </Chatter>
 <Chatter Name="Khang" ID="2">
    <Content  Time="06/05/2014 2:01:40 PM">Send Content</Content>
    <Content  Time="06/05/2014 2:02:00 PM">Recieve Content</Content>
    <Content  Time="06/05/2014 2:20:40 PM">Send Content</Content>
 </Chatter>
 <Chatter Name="Khanh" ID="12">
    <Content Time="06/05/2014 2:03:10 PM">them moi</Content>
 </Chatter>
</Root>

Thank you very much! and my code

public static bool SaveMessage(string name, string content)
    {
        XmlDocument xmldoc = new XmlDocument();
        XmlNodeList xmlnode,xmlOldNode;
        xmldoc.Load(_fileXmlPath);
        xmlnode =xmlOldNode= xmldoc.GetElementsByTagName("Chatter");
        for (int i = 0; i < xmlnode.Count;i++ )
        {
            if (xmlnode[i].Attributes["Name"].Value == name)
            {      
                XmlElement elem = xmldoc.CreateElement("Name", "Content", name);                 
                elem.SetAttribute("Time", DateTime.Now.ToString());
                elem.InnerText = content;
                xmldoc.DocumentElement.AppendChild(elem);
                return true;
            }
        } 
        return false;        
  }

but it not add to xml file

As a rough example, the following should work for you;

//First load the xml into a XDocument (can be a string or file for example)
var doc = XDocument.Load(xml);

//Then try and retrieve the Khang node
XElement khang = (from xml2 in doc.Descendants("Chatter")
                    where xml2.Attribute("Name").Value == "Khang"
                    select xml2).FirstOrDefault();

//If the Khang node exists, create a new Element and add it to the Khang node         
if(khang != null)
{
    XElement newNode = new XElement("Node");
    khang.Add(newNode);
}

In detail:

  1. First load the xml into a XDocument

    var doc = XDocument.Load(xml);

  2. Then try and retrieve the Khang node using Linq To Xml to select the Khang node

    XElement khang = (from xml2 in doc.Descendants("Chatter") where xml2.Attribute("Name").Value == "Khang" select xml2).FirstOrDefault();

  3. Create a new XElement and Add it to the Khang node (if it exists)

    if(khang != null) { XElement newNode = new XElement("Node"); khang.Add(newNode); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM