XDocument
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的更多相关文章
- XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate
namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...
- XDocument获取指定节点
string xmlFile = @"D:\Documents\Visual Studio 2013\Projects\Jesee.Web.Test\ConsoleApplication1\ ...
- C# XML技术总结之XDocument 和XmlDocument
引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...
- XDocument 获取包括第一行的声明(版本、编码)的所有节点
XDocument保存为xml文件的方法如下: XDocument doc = new XDocument( new XDeclaration("1.0","UTF-8& ...
- 将XmlDocument转换成XDocument
XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...
- WPF 关于XDocument(xml) 的部分操作记录
(1)删除xml文件中的一个结点的方法,有如下两种方式(只有存在数据绑定的情况下才会有第二种情况,否则一般是第一种情况): private void DeletePacsNode() { //从xml ...
- .Net 4.0 Convert Object to XDocument
将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...
- XDocument和XmlDocument的区别
刚开始使用Xml的时候,没有注意到XDocument和XmlDocument的区别,后来发现两者还是有一些不同的. XDocument和XmlDocument都可以用来操作XML文档,XDocumen ...
- 05-XML遍历递归显示到TreeView上(XDocument类)
1.XML文件(x1.xml): <?xml version="1.0" encoding="utf-8" ?> <itcast> &l ...
- XmlDocument,XDocument相互转换
XmlDocument,XDocument相互转换 using System; using System.Xml; using System.Xml.Linq; namespace MyTest { ...
随机推荐
- LoadRunner关联需要转义的常见字符
转义字符总结 在做手动关联时,取边界值的时候,会经常用到转义字符,现将转义字符整理如下: \b 退格 \f 换页 \n 换行 \ ...
- iOS 配置支付宝
尽管非常easy,可是对于第一次接触支付宝配置的啊猿.有些细节摸不着头脑.今天就来写一个流程配置. 1.创建一个project,然后再创建一个目录,把支付宝sdk要用到的都拖到目录中.然后拖到proj ...
- 【iOS系列】-UITableViewCell的展开与收缩的实现思路
UITableViewCell的展开与收缩的实现思路 现在项目中很多地方都会用到,所以我这里介绍一种可以复用的思路,这与文章后面的Swift的实现思路不同,具体大家可自行对比. Demo项目地址 开始 ...
- 使用 C# 开发智能手机软件:推箱子(三)
这是"使用 C# 开发智能手机软件:推箱子"系列文章的第三篇.在这篇文章中,介绍 Common/Block.cs 源程序文件. 1 namespace Skyiv.Ben.Pu ...
- 《C++ Primer Plus》学习笔记3
<C++ Primer Plus>学习笔记3 第8章 函数探幽 ============================================================== ...
- Linux操作服务器的初识
1,服务器:顾名思义就是提供服务的机器,(超强性能的一台主机, 100G-500G内存) 2,运维自动化 运维人员, 一个人维护上百台服务器 3,CMDB运维资产管理平台 资产收集, 通过web界面, ...
- wget和curl
1 curl比wget支持更多的协议 2 wget是支持递归的,而curl不支持
- Common non-standard response fields
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#cite_note-52 Common non-standard response f ...
- sqlyog快捷键
Ctrl+M 创建一个新的连接Ctrl+N 使用当前设置新建连接Ctrl+F4 断开当前连接 对象浏览器F5 刷新对象浏览器(默认)Ctrl+B 设置焦点于对象浏览器 SQL 窗口 ...
- Java中使用HttpRequest获取用户真实IP地址端口
import javax.servlet.http.HttpServletRequest; /** * 自定义访问对象工具类 * * 获取对象的IP地址等信息 * @author X-rapido * ...