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 { ...
随机推荐
- MySQL基础笔记(三) 复杂查询
所谓复杂查询,指涉及多个表.具有嵌套等复杂结构的查询.这里简要介绍典型的几种复杂查询格式. 一.连接查询 连接是区别关系与非关系系统的最重要的标志.通过连接运算符可以实现多个表查询.连接查询主要包括内 ...
- uva live 4394 String painter 区间dp
// uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...
- 《Java设计模式》之訪问者模式
訪问者模式是对象的行为模式.訪问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦这些操作须要改动的话,接受这个操作的数据结构则能够保持不变. 分派的概念 变量被声明时的类型叫做变量的静态类 ...
- Windows7 64bits下安装TensorFlow GPU版本(图文详解)
不多说,直接上干货! Installing TensorFlow on Windows的官网 https://www.tensorflow.org/install/install_windows 首先 ...
- 百度接口通过ip获取用户所在地
/** * 百度接口 * 通过用户ip获取用户所在地 * @param userIp * @return */ public static String get ...
- 【CTF】后续深入学习内容
1.i春秋 https://www.ichunqiu.com/course/451 搜索black hat,可以看到黑帽大会的内容.免费. 2.wireshark 基础篇 1)由于Wireshark是 ...
- 查询局域网内全部电脑IP和mac地址等信息
怎么查询局域网内全部电脑IP和mac地址等信息_百度经验 https://jingyan.baidu.com/article/54b6b9c0348e432d583b47c1.html 枚举ping ...
- ABAP WEBRFC
通过WEBRFC实现在网页下载SMW0上传的文件 FUNCTION zhr_download_test. *"---------------------------------------- ...
- 安装linux各种桌面环境
1.安装kde ①添加 KDE SC 4.11 库 打开一个终端窗口,在终端窗口中输入如下命令: sudo add-apt-repository ppa:kubuntu-ppa/backports 回 ...
- go string []byte相互转换
string 不能直接和byte数组转换 string可以和byte的切片转换 1,string 转为[]byte var str string = "test" var data ...