创建XML文件时,无法将非空白字符添加到内容中

ten*_*779 2 c# xml linq-to-xml

我在这里创建一个简单的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");
Run Code Online (Sandbox Code Playgroud)

L.B*_*L.B 9

问题出在这一行

XDocument myDoc = new XDocument("Root");
Run Code Online (Sandbox Code Playgroud)

将您的代码更改为:

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

myDoc.Save("foo.xml");
Run Code Online (Sandbox Code Playgroud)