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

时间:2013-10-06 14:55:24

标签: c# linq-to-xml

我的XML文件格式为:

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

 </TestFile>"

这里的根元素是“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>"

如何获得所需的输出?

1 个答案:

答案 0 :(得分:0)

试试这个

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

中添加下一个参数中添加一些值

Exmaple

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"));