日期:2014-05-16  浏览次数:21192 次

加载xml失败,提示"根级别上的数据无效。 第 1 行,位置 1。"
我写了几行代码来试图加载一个xml文件:

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(@"D:\Users\XMLFile1.xml");
                Console.WriteLine("LoadXML ok");
                XmlElement element = doc.DocumentElement;
                string text=FormatText(element,""," ");
                Console.WriteLine(text);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

运行报错:
根级别上的数据无效。 第 1 行,位置 1。
而D:\Users\XMLFile1.xml内容如下:

<?xml version="1.0" encoding="utf-8" ?> 
<stories>
  <story>
    <title>A House in Aungier Street</title>
    <author>
      <name>Sheridan Le Fanu</name>
      <nationality>Irish</nationality>
    </author>
    <rating>eerie</rating>
   </story>
   <story>
     <title>The Signalman</title>
     <author>
      <name>Charles Dickens</name>
      <nationality>English</nationality>
     </author>
     <rating>atmospheric</rating>
   </story>
  <story>
     <title>The Turn of the Screw</title>
     <author>
       <name>Henry James</name>
       <nationality>American</nationality>
     </author>
     <rating>a bit dull</rating>
  </story>
</stories>

错误到底在什么地方呢? 检查了好久都没有查到原因。各位帮忙看下哈!
------解决方案--------------------
doc.Load(“文件名”)
------解决方案--------------------
这是 loadxml

using System;
using System.Xml;

public class Sample {

  public static void Main() {
 
    // Create the XmlDocument.
    XmlDocument doc