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 { ...
随机推荐
- nagios 安装配置(包含nrpe端)全 (三)
四.系统的配置: 1.介绍 在配置过程中涉及到的几个定义有:主机.主机组,服务.服务组.联系人.联系人组,监控时间.监控命令等. 最重要的有四点: 第一:定义监控哪些主机.主机组.服务和服务组: 第二 ...
- Android进阶图片处理之三级缓存方案
图片的三级缓存 一.概述 一開始在学习Android的时候.处理图片的时候,每次获取图片都是直接从网络上面载入图片. 可是在开发项目的过程中,每次点击进入app里面,图片都要慢慢的再一次从网络上面载入 ...
- 李洪强iOS开发之性能优化技巧
李洪强iOS开发之性能优化技巧 通过静态 Analyze 工具,以及运行时 Profile 工具分析性能瓶颈,并进行性能优化.结合本人在开发中遇到的问题,可以从以下几个方面进行性能优化. 一.view ...
- (WPF)附加属性
<Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...
- js调试记录,将客户的调试信息保存到服务器端的一个小方法。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android:内存控制及OOM处理
1. OOM(内存溢出)和Memory Leak(内存泄露)有什么关系? OOM可能是因为Memory Leak,也可能是你的应用本身就比较耗内存(比如图片浏览型的).所以,出现OOM不一定是Me ...
- leetCode(26):Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 清理一下电脑垃圾,打开Eclipse发现左边的所有项目消失了
使用360清理一下电脑垃圾,打开Eclipse发现左边的所有项目消失了,但在相应的workspace能够找到项目,这个问题已经是第三次遇到了(预计是被360清理掉Eclipse的一些部署或配置文件所导 ...
- 6.游戏特别离不开脚本(3)-JS脚本操作java(直接解析JS公式,并非完整JS文件或者函数)
engine.put("usList", us); engine.put("obj", new JSModifiedJava()) ; 取个变量名就put进去 ...
- File to byte[] in Java
File to byte[] in Java - Stack Overflow https://stackoverflow.com/questions/858980/file-to-byte-in-j ...