如何使用 LinQ 在 XML 文件中设置 thr 根元素的值

本文关键字:thr 元素 设置 LinQ 何使用 XML 文件 | 更新日期: 2023-09-27 18:34:03

我的XML文件的形式是:

 "<TestFile fileext="C:'TestFiles'TestFile2.txt">
 </TestFile>"

这里的根元素是"测试文件"。我的任务是创建此形式的新 XML,我正在使用的查询是:

  XDocument doc = new XDocument(
           new XDeclaration("1.0", "utf-8", "yes"),
           new XComment("This is a medtata file of the file " + args[0]),
           new XElement("TestFile",path));

但不是

 "<TestFile fileext="C:'TestFiles'TestFile2.txt">
 </TestFile>"

,输出为

 "<TestFile>C:'TestFiles'TestFile2.txt</TestFile>"

如何获得所需的输出?

如何使用 LinQ 在 XML 文件中设置 thr 根元素的值

试试这个

XDocument doc = new XDocument(
       new XDeclaration("1.0", "utf-8", "yes"),
       new XComment("This is a medtata file of the file " + args[0]),
       new XElement("TestFile",new XAttribute("filetext", path)));

如果要在 XElement 中添加一些值,请在 XElement 中添加下一个参数

示例

XDocument doc = new XDocument(
           new XDeclaration("1.0", "utf-8", "yes"),
           new XComment("This is a medtata file of the file " + args[0]),
           new XElement("TestFile",new XAttribute("filetext", path), "something"));