XDocument学习(Winform)

 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; namespace _01XDocument
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//2017-12-14 再次写一遍
Dictionary<string, Person> dict = new Dictionary<string, Person>(); private void Form1_Load(object sender, EventArgs e)
{
XDocument xDoc = XDocument.Load("ListPerson.xml");
XElement root = xDoc.Root; foreach (var item in root.Elements("Person"))
{
Person model = new Person();
model.Id = item.Attribute("Id").Value;
model.Name = item.Element("Name").Value;
model.Age = int.Parse(item.Element("Age").Value);
model.Email = item.Element("Email").Value; //加载数据
dict.Add(model.Id,model);
listBox.Items.Add(model);
}
} private void btnAdd_Click(object sender, EventArgs e)
{
Person model = new Person();
model.Id = txtId.Text;
model.Name = txtName.Text;
model.Age = int.Parse(txtAge.Text);
model.Email = txtEmail.Text; if (btnAdd.Text=="修改")
{
dict[model.Id] = model;
listBox.Items[listBox.SelectedIndex] = model;
}
else
{
//增加
dict.Add(model.Id,model);
listBox.Items.Add(model);
}
//清除文本框的值
ClearTextBox();
btnAdd.Text = "添加";
txtId.Enabled = true;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 清空
/// </summary>
private void ClearTextBox()
{
foreach (var item in Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = string.Empty;
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//关闭时候,将Dictionary中数据保存在ListPerson.xml
XDocument xDoc = new XDocument();
XElement root = new XElement("ListPerson");
xDoc.Add(root); foreach (KeyValuePair<string,Person> item in dict)
{
XElement person = new XElement("Person");
person.SetAttributeValue("Id",item.Value.Id);
person.SetElementValue("Name",item.Value.Name);
person.SetElementValue("Age",item.Value.Age);
person.SetElementValue("Email",item.Value.Email); root.Add(person);
}
xDoc.Save("ListPerson.xml");
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
Person model = listBox.SelectedItem as Person;
txtId.Text = model.Id;
txtName.Text = model.Name;
txtAge.Text = model.Age.ToString();
txtEmail.Text = model.Email; btnAdd.Text = "修改";
txtId.Enabled = false;
} /// <summary>
/// 构造Person类
/// </summary>
public class Person
{
public Person()
{ }
public Person(string id, string name, int age, string email)
{
this.Id = id;
this.Name = name;
this.Age = age;
this.Email = email;
}
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public override string ToString()
{
//return base.ToString();
return this.Name;
}
} }
}

XDocument的更多相关文章

  1. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  2. XDocument获取指定节点

    string xmlFile = @"D:\Documents\Visual Studio 2013\Projects\Jesee.Web.Test\ConsoleApplication1\ ...

  3. C# XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  4. XDocument 获取包括第一行的声明(版本、编码)的所有节点

    XDocument保存为xml文件的方法如下: XDocument doc = new XDocument( new XDeclaration("1.0","UTF-8& ...

  5. 将XmlDocument转换成XDocument

    XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...

  6. WPF 关于XDocument(xml) 的部分操作记录

    (1)删除xml文件中的一个结点的方法,有如下两种方式(只有存在数据绑定的情况下才会有第二种情况,否则一般是第一种情况): private void DeletePacsNode() { //从xml ...

  7. .Net 4.0 Convert Object to XDocument

    将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...

  8. XDocument和XmlDocument的区别

    刚开始使用Xml的时候,没有注意到XDocument和XmlDocument的区别,后来发现两者还是有一些不同的. XDocument和XmlDocument都可以用来操作XML文档,XDocumen ...

  9. 05-XML遍历递归显示到TreeView上(XDocument类)

    1.XML文件(x1.xml): <?xml version="1.0" encoding="utf-8" ?> <itcast> &l ...

  10. XmlDocument,XDocument相互转换

    XmlDocument,XDocument相互转换 using System; using System.Xml; using System.Xml.Linq; namespace MyTest { ...

随机推荐

  1. kill mediaserver脚本

    #!/bin/bash adb shell kill $(adb shell ps | grep mediaserver | awk '{print $2}') adb shell pm clear ...

  2. Robotframework集成jenkins执行用例

    Robotframework+jenkins配置 假设我们完成了一个模块的用例设计,可是想晚上9点或凌晨运行,这时候该怎么实现呢?jenkins可以很好解决我们的疑难. Jenkins安装 这里简单说 ...

  3. boost的内存管理

    smart_ptr raii ( Resource Acquisition Is Initialization ) 智能指针系列的都统称为smart_ptr.包含c++98标准的auto_ptr 智能 ...

  4. ffmpeg转码本地文件(一)

    ffmpeg转码本地文件(一) 实现目标:输入本地文件.实现本地文件转码,里面包括mux层转码,codec层转码,视频格式转换,音频重採样等功能,功能点请看凝视.注意:凝视非常重要. #ifndef ...

  5. 2016/2/25 1、<表单验证<form></form> 2、正则表达式 3、事件

    1.<表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电 ...

  6. Spring简单实现数据源的动态切换

    Spring简单实现数据源的动态切换: 1. 创建一个数据源切换类: 2. 继承AbstractRoutingDataSource,创建多数据源路由类,并注入到spring的配置文件中: 3. AOP ...

  7. mybatis批量操作数据

    批量查询语句: List<MoiraiProductResource> selectBatchInfo(List<Long> idList); <!-- 批量查询 --& ...

  8. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

  9. html5--项目实战-仿天猫(移动端页面)

    html5--项目实战-仿天猫(移动端页面) 总结: 1.标准搜索栏的做法:这里是弹性布局,放大镜和小话筒是background img 2.手机尾部导航做法:这是一个个 li 标签,每个li标签占% ...

  10. aria2 for mac

    本文是在安装好homebrew前提下 brew install aria2 然后配置参数 cd ~ mkdir .aria2 cd .aria2 vim aria2.conf 以下配置粘贴进去,红色需 ...