tags:

views:

55

answers:

2
    private const string filename = "output.xml";

    /// <summary>
    /// Create file code
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        XmlTextWriter newXmlFile = new XmlTextWriter(filename,System.Text.Encoding.Default);    
        newXmlFile.WriteStartDocument();            
        newXmlFile.WriteStartElement("Begin","");
        newXmlFile.WriteStartElement("One","");
        newXmlFile.WriteFullEndElement();
        newXmlFile.WriteEndElement();
        newXmlFile.Flush();
        newXmlFile.Close();
    }
    /// <summary>
    /// Insert New code
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    private void button2_Click(object sender, EventArgs e)
    {
       XmlDocument XMLDom = new XmlDocument();
        XMLDom.Load(filename);
        XmlNode newXMLNode = XMLDom.SelectSingleNode("Begin");
        XmlNode childNode = XMLDom.CreateNode(XmlNodeType.Element,"One","");
        XmlAttribute newAttribute = XMLDom.CreateAttribute("name","sree","");
        childNode.Attributes.Append(newAttribute);
        newXMLNode.AppendChild(childNode);
    }
}

}

+1  A: 

Look at Linq to XML and the System.Xml namespace.

Oded
A: 

Like Oded says: Linq to XML should get you there...

These classes are the important ones:

  • XDocument
    • `var doc = XDocument.Load("myfilepath")
  • XElement
  • XAttribute
Daren Thomas