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 { ...
随机推荐
- Windows环境下QWT安装及配置
** 1.QWT下载路径 **:https://sourceforge.net/projects/qwt/ 主要下载这三个文件:qwt-6.1.2.zip.qwt-6.1.2.pdf,qwt-6.1. ...
- List<Guid?> a = new List<Guid?>();
正常写法 泛型 类型 为 Guid? List<Guid?> a = new List<Guid?>(); Guid? b = null; Nullable<Guid&g ...
- noteexpress使用指南
软件功能:在写论文时直接调用参考数据并输出正规的格式. (以下简称NE) A.下载安装 下载地址:Note-express - Bibliography Software 选择相应的学校进行下载,相 ...
- windows 目录空格
Window下安装Scala出现:此时不应有 \scala\bin\..\lib\jline-2.14.5.jar 原因很简单,scala默认安装到了Program Files (x86)文件夹下,目 ...
- Deep Learning 30: 卷积理解
一.深度卷积神经网络学习笔记(一): 1. 这篇文章以贾清扬的ppt说明了卷积的实质,更说明了卷积输出图像大小应该为: 假设输入图像尺寸为W,卷积核尺寸为F,步幅(stride)为S(卷积核移动的步幅 ...
- (21) java web的struts2框架的使用
在javaweb开发过程中,如果只使用servlet,jdbc,jsp进行开发,也可以遵从MVC的模式,这时候,servlet相当于control层,属于负责处理业务逻辑的控制器,同时也需要对获取和返 ...
- Linux内核--基于Netfilter的内核级包过滤防火墙实现
测试内核版本:Linux Kernel 2.6.35----Linux Kernel 3.2.1 原创作品,转载请标明http://blog.csdn.net/yming0221/article/de ...
- POJ - 3041 Asteroids(最小点覆盖数)
1.有一个n*n的矩阵,在矩阵上有k个行星,用武器射击一次可以消灭一行或者一列的行星,求消灭所有的行星的最少射击次数. 2.最小点覆盖数 = 最大匹配数 主要在于转化:看图: 这样,在建成的二分图中, ...
- 关于ArcGis for javascript的引用天地图
1. 在引用天地图时, 我们要自定义一个相关的比例尺转换类 const tileInfoObj = { rows: 256, cols: 256, compressionQuality: 0, ori ...
- get与post提交方式区别?
1.get <!--表单数据作为HTTP GET请求发送给action 规定的URL,并将数据附加在URL之后,由客户端直接发送给服务器.表单数据不能太长,也不能含有非ASCII码的字符--&g ...