如何使用c#Visual Studio更改XML文件中的值
我正在尝试更改XML文件中的值。这是我的XML“person.XML”文件:如何使用c#Visual Studio更改XML文件中的值,c#,xml,visual-studio,C#,Xml,Visual Studio,我正在尝试更改XML文件中的值。这是我的XML“person.XML”文件: 1. 马里亚诺 意大利语 36 2. 约翰 史密斯 32 3. 上下快速移动 莱基 50 4. 帕特里克 柯林斯 63 我想让我的程序做一些事情: 查找写在textBox2->Text中的名称。然后,如果名称存在,我想将其更改为写在textBox3->Text中的名称 如果CheckBoxDelete设置为true,则删除(如果找到)整个人 我知道如何创建这样一个XML文件(在某处找到了一个示例),但我找不到一个解
1.
马里亚诺
意大利语
36
2.
约翰
史密斯
32
3.
上下快速移动
莱基
50
4.
帕特里克
柯林斯
63
我想让我的程序做一些事情:
public class Person
{
[XmlElement]
public int Number { get; set; }
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string LastName { get; set; }
[XmlElement]
public int Age { get; set; }
public override string ToString() => $"{Name} {LastName} is {Age}";
}
[XmlRoot("Table")]
public class RootObject
{
[XmlElement("Person")]
public List<Person> Persons;
}
class Program
{
static void Main(string[] args)
{
var xmlSer = new XmlSerializer(typeof(RootObject));
using var fs = new FileStream("input.xml", FileMode.Open);
var doc = (RootObject)xmlSer.Deserialize(fs);
foreach (var p in doc.Persons)
{
System.Console.WriteLine(p);
}
// do whatever with the RootObject instance
}
}
公共类人物
{
[XmlElement]
公共整数{get;set;}
[XmlElement]
公共字符串名称{get;set;}
[XmlElement]
公共字符串LastName{get;set;}
[XmlElement]
公共整数{get;set;}
公共重写字符串ToString()=>$“{Name}{LastName}是{Age}”;
}
[XmlRoot(“表”)]
公共类根对象
{
[XmlElement(“个人”)]
公众人士名单;
}
班级计划
{
静态void Main(字符串[]参数)
{
var xmlSer=新的XmlSerializer(typeof(RootObject));
使用var fs=newfilestream(“input.xml”,FileMode.Open);
var doc=(RootObject)xmlSer.Deserialize(fs);
foreach(单据人员中的var p)
{
系统控制台写入线(p);
}
//对RootObject实例执行任何操作
}
}
< /> > XML是对象,因此在创建更多的POCs之前考虑重新使用现有对象。
下面的代码展示了如何在XML中查找、删除和更新元素。花点时间学习XPath,它非常强大、灵活,几乎可以在所有平台上使用
using System;
using System.Threading.Tasks;
using System.Xml;
namespace testconsole
{
class Program
{
public static string strFileName = "c:\\temp\\test.xml";
static void Main(string[] args) {
XmlDocument xml = new XmlDocument();
xml.Load(strFileName);
string strMatchName = "Mariano";
string strXPath = "/Table/Person[Name='" + strMatchName + "']";
XmlElement ndPerson = (XmlElement)xml.SelectSingleNode(strXPath);
if (ndPerson != null) {
// Delete if the person is found
ndPerson.ParentNode.RemoveChild(ndPerson);
}
string strNewName = "Bob";
strXPath = "/Table/Person/Name[.='" + strNewName + "']";
XmlElement ndName = (XmlElement)xml.SelectSingleNode(strXPath);
if (ndName != null) {
ndName.InnerText = "Robert"; // new name
}
xml.Save(strFileName);
}
}
}
您可以使用XDocument替换该值,并根据姓名删除此人
代码:
此外,您的xml文件有一个错误,LastName
应该是LastName
,没有空格。这里有一些选择节点的示例:在这里删除它:您可以使用、尝试这个或@RolandoMedina命题来阅读关于linq to xml的内容,我们将通过包含一些代码来帮助您。“删除”是不可能的直接在文件中创建记录,但显然可以将整个文档读入内存,对其进行反序列化、修改,然后再次将其序列化为XML。如前所述:XDocument、LINQ和XMLReader将为您提供帮助。我正在尝试修改您的btndelete_Click代码,以便如果有两个人的名字相同,但姓氏不同,则只删除一个。如何做到这一点?用于删除的数据存储在textBoxReplaceFirstName和textBoxReplaceLastName@Mariusz,我已经修改了我的代码,你可以检查它是否适合你。
using System;
using System.Threading.Tasks;
using System.Xml;
namespace testconsole
{
class Program
{
public static string strFileName = "c:\\temp\\test.xml";
static void Main(string[] args) {
XmlDocument xml = new XmlDocument();
xml.Load(strFileName);
string strMatchName = "Mariano";
string strXPath = "/Table/Person[Name='" + strMatchName + "']";
XmlElement ndPerson = (XmlElement)xml.SelectSingleNode(strXPath);
if (ndPerson != null) {
// Delete if the person is found
ndPerson.ParentNode.RemoveChild(ndPerson);
}
string strNewName = "Bob";
strXPath = "/Table/Person/Name[.='" + strNewName + "']";
XmlElement ndName = (XmlElement)xml.SelectSingleNode(strXPath);
if (ndName != null) {
ndName.InnerText = "Robert"; // new name
}
xml.Save(strFileName);
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string filename = "D:\\test.xml";
private void BtnRePlace_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(filename);
doc.Descendants("Person").Descendants("Name").Where(i => i.Value == txtReplaceFirstName.Text).FirstOrDefault().SetValue(txtReplaceLastName.Text);
doc.Save(filename);
}
private void btndelete_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(filename);
doc.Descendants("Person").Where(i => i.Element("Name").Value == txtReplaceFirstName.Text&& i.Element("LastName").Value == txtReplaceLastName.Text).FirstOrDefault().Remove();
doc.Save(filename);
}
}