C# 为特定id c插入新的xml节点#
如何添加id为文本框中特定值的新节点? 这是我的xml:C# 为特定id c插入新的xml节点#,c#,xml,linq,C#,Xml,Linq,如何添加id为文本框中特定值的新节点? 这是我的xml: <Students> <Student> <id>111</id> <Value>1</Value> <information> <data DateTime="02.04.2014 13:00:00" Value="1"/> </information> <
<Students>
<Student>
<id>111</id>
<Value>1</Value>
<information>
<data DateTime="02.04.2014 13:00:00" Value="1"/>
</information>
</Student>
</Students>
111
1.
所以我的问题是,我有一个文本框,在其中输入学生id。单击一个按钮后,我想在信息节点中添加一个新节点,包含该时刻的属性日期和时间
另一件事是,我希望节点中的innerText在每次单击时从1更改为0,反之亦然。因此,这将是节点中的第二个属性
下次我单击时,如果它想添加一个新节点,它会添加这个
<information>
<data DateTime="02.04.2014 13:00:00" Value="1"/>
<data DateTime="02.04.2014 14:00:00" Value="0"/>
</information>
如何使用XmlLinq实现这一点
var xdoc = XDocument.Load(path_to_xml);
var student = xdoc.Root.Elements("Student")
.FirstOrDefault(s => (int)s.Element("id") == id);
if (student != null) // check if student was found
{
var info = student.Element("information");
if (info == null) // check if it has information element
{
info = new XElement("information");
student.Add(info); // create and add information element
}
var lastValue = info.Elements("data")
.OrderBy(d => (DateTime)d.Attribute("DateTime"))
.Select(d => (int)d.Attribute("Value"))
.LastOrDefault(); // get latest value
// create new data element
var data =
new XElement("data",
new XAttribute("DateTime", DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss")),
new XAttribute("Value", lastValue == 0 ? 1 : 0));
info.Add(data); // add data element to information element
xdoc.Save(path_to_xml); // save file
}
结果:
<Students>
<Student>
<id>111</id>
<Value>1</Value>
<information>
<data DateTime="02.04.2014 13:00:00" Value="1" />
<data DateTime="02.05.2014 00:40:18" Value="0" />
</information>
</Student>
</Students>
111
1.
c#有一种方法可以让您轻松做到这一点:
XmlNode.InsertAfter方法
链接到实际页面:
如果不想点击,请编写示例:
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
XmlNode root = doc.DocumentElement;
//Create a new node.
XmlElement elem = doc.CreateElement("price");
elem.InnerText="19.95";
//Add the node to the document.
root.InsertAfter(elem, root.FirstChild);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
使用系统;
使用System.IO;
使用System.Xml;
公共类样本{
公共静态void Main(){
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“”)+
《傲慢与偏见》+
"");
XmlNode root=doc.DocumentElement;
//创建一个新节点。
XmlElement elem=doc.CreateElement(“价格”);
elem.InnerText=“19.95”;
//将节点添加到文档中。
root.InsertAfter(elem,root.FirstChild);
WriteLine(“显示修改后的XML…”);
单据保存(控制台输出);
}
}
如果需要查找要插入的特定节点,请检查此项
XMLnode和Xelement之间存在差异,OP需要一个xmlLinq解决方案+对不起,你能详细说明一下吗?@SergeyBerezovskiy你知道,有时候人们只是按下按钮,而不理解问题和建议的答案哈,对不起,出于某种原因,我的想法在xml和linq之间划了一个斜线。我将看看是否可以找到更好的答案如何添加条件,如果id不在xml中,例如显示一个消息框?我猜它将出现在var student=xdoc.Root.Elements(“student”).FirstOrDefault(s=>(int)s.Element(“id”)==id)中@用户2962759添加
else
part:if(student!=null){…}else{show message box}