0

我的 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>"

如何获得所需的输出?

4

1 回答 1

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 中添加一些值,请添加下一个参数

示例

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"));
于 2013-10-06T15:27:25.427 回答