0

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

1

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

i got it, it run exactly. thank you very much! Can you share me a website to learn about xml or gool tutorial ? i don't know start from where to have strong base.
@DatLieu Something like this appears to be a reasonable place to start: dotnetcurry.com/showarticle.aspx?ID=564

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.