我需要有关如何根据时间戳“自动”删除节点的帮助。特定日期由用户在 xml 文档中定义,例如17/9/2006 有人可以给我举个例子吗? 提前致谢!
<root>
<element>
</element>
<timestamp time="2016-09-16T13:45:30">
</timestamp>
<--how do I delete element based on the given timestamp?-->
</root>
//UNTESTED CODE
XDocument doc = XDocument.Load("time.xml");
var name = doc.Descendants("root")
.Where(n => n.Attribute("time").Value == "2016-09-16T13:45:30")
.Select(n => (string)n)
.First();
<--how can I delete it based on timestamp-->
name.Element("element").Remove();
最佳答案
解析 ISO 8601 日期/时间格式:
string input = "2016-09-16T13:45:30";
DateTime converted = DateTime.Parse(input, null, DateTimeStyles.RoundtripKind);
一旦日期转换为 DateTime 类型,您就可以使用它来识别您希望删除的节点(强烈建议为此使用 LinQ)。
关于c# - XML根据时间戳删除节点C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39508178/