日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2021-3-13 xml的增删改查

發布時間:2023/10/18 编程问答 127 如意码农
生活随笔 收集整理的這篇文章主要介紹了 2021-3-13 xml的增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  public void XmlAdd(string filename, List<People> pList)
{
try
{
List<People> peoples = XmlDeSerializer(filename);
foreach (var item in pList)
{
People people = new People();
people.Name = string.IsNullOrEmpty(item.Name) ? "" : item.Name;
people.Id = string.IsNullOrEmpty(item.Id) ? "" : item.Id;
people.Age = string.IsNullOrEmpty(item.Age) ? "" : item.Age;
people.Sex = string.IsNullOrEmpty(item.Sex) ? "" : item.Sex;
peoples.Add(people);
}
XmlSerializer x = new XmlSerializer(typeof(List<People>));
TextWriter writer = new StreamWriter(filename);
x.Serialize(writer, peoples);
writer.Dispose();
}
catch (Exception ex)
{ }
}
/// <summary>
/// xml編輯
/// </summary>
/// <param name="filename"></param>
/// <param name="people"></param>
public void XmlEdit(string filename, List<People> pList, int index)
{
try
{
XmlDelete("User", index);
List<People> peoples = XmlDeSerializer(filename);
foreach (var item in pList)
{
People people = new People();
people.Name = string.IsNullOrEmpty(item.Name) ? "" : item.Name;
people.Id = string.IsNullOrEmpty(item.Id) ? "" : item.Id;
people.Age = string.IsNullOrEmpty(item.Age) ? "" : item.Age;
people.Sex = string.IsNullOrEmpty(item.Sex) ? "" : item.Sex;
peoples.Insert(index, people);
}
XmlSerializer x = new XmlSerializer(typeof(List<People>));
TextWriter writer = new StreamWriter(filename);
x.Serialize(writer, peoples);
writer.Dispose();
}
catch (Exception ex)
{ }
}
/// <summary>
/// 刪除列表
/// </summary>
/// <param name="filename"></param>
/// <param name="pList"></param>
public void XmlDelete(string filename, int index = 0)
{
try
{
List<People> peoples = XmlDeSerializer(filename);
peoples.RemoveAt(index);
XmlSerializer x = new XmlSerializer(typeof(List<People>));
TextWriter writer = new StreamWriter(filename);
x.Serialize(writer, peoples);
writer.Dispose();
}
catch (Exception ex)
{ }
}
/// <summary>
/// 顯示列表
/// </summary>
/// <param name="filename"></param>
/// <param name="pList"></param>
public List<People> XmlDeSerializer(string filename)
{
try
{
var mySerializer = new XmlSerializer(typeof(List<People>));
var myFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
if (myFileStream.Length > 0)
{
var myObject = (List<People>)mySerializer.Deserialize(myFileStream);
return myObject;
} myFileStream.Dispose();
return new List<People>();
}
catch (Exception ex)
{
return new List<People>();
}
}

以上是可以放在xmlHelp直接調用的方法

  public class People
{
public string Name;
public string Sex;
public string Age;
public string Id;
public People() { }
public People(string Name, string Sex, string Age, string Id)
{
this.Name = Name;
this.Sex = Sex;
this.Age = Age;
this.Id = Id;
} }

總結

以上是生活随笔為你收集整理的2021-3-13 xml的增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。