gpt4 book ai didi

c# - 创建 XML 文件时不能将非空白字符添加到内容中

转载 作者:行者123 更新时间:2023-11-30 19:26:15 24 4
gpt4 key购买 nike

我在这里创建一个简单的 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");

最佳答案

问题出在这一行

XDocument myDoc = new XDocument("Root");

将您的代码更改为:

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

myDoc.Save("foo.xml");

关于c# - 创建 XML 文件时不能将非空白字符添加到内容中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com