【问题标题】:How to set value of thr root element in XML file using LinQ?如何使用 LinQ 设置 XML 文件中根元素的值?
【发布时间】:2013-10-06 23:27:25
【问题描述】:

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

我怎样才能得到想要的输出?

【问题讨论】:

标签: c# linq-to-xml


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多