我需要使用xml执行添加,删除,更新,读取数据。这段代码有probs吗?

时间:2010-07-21 06:19:19

标签: c# xml

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

}

2 个答案:

答案 0 :(得分:1)

查看Linq to XMLSystem.Xml命名空间。

答案 1 :(得分:0)

就像Oded所说:Linq to XML应该让你到那里......

这些课程很重要:

  • XDocument
    • `var doc = XDocument.Load(“myfilepath”)
  • XElement
  • XAttribute