C# 错误:XML 声明必须是文档中的第一个节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11959304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:50:48  来源:igfitidea点击:

Error: The XML declaration must be the first node in the document

c#.netxml

提问by user841311

I am getting "Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it" error while trying to load xml. Both my C# code and contents of XML file are given below. XML definition exists in Line 6 of the xml file and hence the error.

我在尝试加载 xml 时收到“意外的 XML 声明。XML 声明必须是文档中的第一个节点,并且不允许在它之前出现空格字符”错误。下面给出了我的 C# 代码和 XML 文件的内容。XML 定义存在于 xml 文件的第 6 行,因此出现错误。

I can not control what's there in the xml file so how can I edit/rewrite it using C# such that xml declaration comes first and then the comments to load it without any error!

我无法控制 xml 文件中的内容,所以我如何使用 C# 编辑/重写它,以便首先出现 xml 声明,然后是加载它的注释而不会出现任何错误!

//xmlFilepath is the path/name of the xml file passed to this function
static function(string xmlFilepath)
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
readerSettings.IgnoreWhitespace = true;
XmlReader reader = XmlReader.Create(XmlFilePath, readerSettings);
XmlDocument xml = new XmlDocument();
xml.Load(reader);
}

XmlDoc.xml

文档文件

<!-- Customer ID: 1 -->
<!-- Import file: XmlDoc.xml -->
<!-- Start time: 8/14/12 3:15 AM -->
<!-- End time: 8/14/12 3:18 AM -->

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
-----

采纳答案by KeithS

As the error states, the first five characters of an XML document should be <?xml. No ifs, ands or buts. The comments you have above the opening XML tag are illegal; they must go inside the XML tag (because the comment structure is itself defined by the XML standard and so is meaningless outside the main XML tags).

正如错误所述,XML 文档的前五个字符应该是<?xml. 没有如果,和或但是。您在 XML 开头标记上方的注释是非法的;它们必须在 XML 标签内部(因为注释结构本身是由 XML 标准定义的,因此在主要 XML 标签之外是没有意义的)。

EDIT:Something like this should be able to rearrange the rows, given the file format from the OP:

编辑:鉴于来自 OP 的文件格式,这样的事情应该能够重新排列行:

var lines = new List<string>();

using (var fileStream = File.Open(xmlFilePath, FileMode.Open, FileAccess.Read))
   using(var reader = new TextReader(fileStream))
   {
      string line;
      while((line = reader.ReadLine()) != null)
         lines.Add(line);
   }   

var i = lines.FindIndex(s=>s.StartsWith("<?xml"));
var xmlLine = lines[i];
lines.RemoveAt(i);
lines.Insert(0,xmlLine);

using (var fileStream = File.Open(xmlFilePath, FileMode.Truncate, FileAccess.Write)
   using(var writer = new TextWriter(fileStream))
   {
      foreach(var line in lines)
         writer.Write(line);

      writer.Flush();
   } 

回答by SLaks

That is not valid XML.

那不是有效的 XML。

As the error clearly states, the XML declaration (<?xml ... ?>) must come first.

正如错误明确指出的那样,XML 声明 ( <?xml ... ?>) 必须放在最前面

回答by Misheeta

I'm using the following function to remove whitespace from xml:

我正在使用以下函数从 xml 中删除空格:

public static void DoRemovespace(string strFile)
    {
        string str = System.IO.File.ReadAllText(strFile);
        str = str.Replace("\n", "");
        str = str.Replace("\r", "");
        Regex regex = new Regex(@">\s*<");
        string cleanedXml = regex.Replace(str, "><");
        System.IO.File.WriteAllText(strFile, cleanedXml);

    }

回答by MacGyver

Don't put any comments in the beginning of your file!

不要在文件的开头添加任何注释!