【问题标题】:Non white space characters cannot be added to content when creating XML File创建 XML 文件时不能将非空白字符添加到内容中
【发布时间】:2014-06-10 23:55:54
【问题描述】:

我在这里创建了一个简单的 XML 文件,当我这样做时,我收到了关于无法将非空白字符添加到内容的错误。

在构造函数中,我传递了一个字符串来创建称为 Root 的第一个节点。这会导致代码中出现非空白错误。有没有人看到问题。这是 Visual Studio 上的 C#。

XDocument myDoc = new XDocument("Root");
            myDoc.Add(
           Enumerable.Range(0, 6).Select(i =>

                            new XElement("Entry",
                                 new XAttribute("Address", "0123"),
                                 new XAttribute("default", "0"),

                            new XElement("Descripion", "here is the description"),
                            new XElement("Data", "Data goes here ")

                    )));

            myDoc.Save("foo.xml");

【问题讨论】:

  • 你的范围应该是 (0,5),因为你只有 5 个元素吗?
  • 不幸的是,这并没有解决它,但是当我在第一行创建类 XDocument 的实例时,编译器指出了问题。

标签: c# xml linq-to-xml


【解决方案1】:

问题出在这一行

XDocument myDoc = new XDocument("Root");

将代码更改为:

XElement root = new XElement("root");
XDocument myDoc = new XDocument(root);
root.Add( Enumerable.Range...... );

myDoc.Save("foo.xml");

【讨论】:

  • @user3670048 如果你喜欢它,请阅读this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2013-01-14
相关资源
最近更新 更多