C#中XML的操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace XML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XElement xPerson = new XElement("Persons");
            XElement xPerson1 = new XElement("Person");
            XElement xPerson2 = new XElement("Person");
            XElement xPerson3 = new XElement("Person");

            xPerson1.Value = "efewfew";

            xPerson.Add(xPerson1);
            xPerson.Add(xPerson2);
           

            string xml = xPerson.ToString();
            MessageBox.Show(xml);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using(Stream stream=File.OpenRead(@"c:\1.xml"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    XDocument xdoc = XDocument.Load(reader);
                    string v1= xdoc.Root.Attribute("f").Value;
                    int i= xdoc.Nodes().Count();
                    IEnumerable<XElement> connstrs=xdoc.Root.Elements("jiedian");
                    foreach (XElement xContent in connstrs)
                    {
                        var jiedian=xdoc.Descendants("jiedian"); //唯一的节点还行
                    }
                }
            }
           
        }
    }
}

 

posted @ 2013-06-04 10:02  张国朋  阅读(193)  评论(0编辑  收藏  举报