I need to perform add, delete, update, read data using xml. this code has probs? [closed]
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 childN开发者_运维问答ode = XMLDom.CreateNode(XmlNodeType.Element,"One","");
XmlAttribute newAttribute = XMLDom.CreateAttribute("name","sree","");
childNode.Attributes.Append(newAttribute);
newXMLNode.AppendChild(childNode);
}
}
}
Look at Linq to XML and the System.Xml
namespace.
Like Oded says: Linq to XML should get you there...
These classes are the important ones:
XDocument
- `var doc = XDocument.Load("myfilepath")
XElement
XAttribute
精彩评论