错误:XML声明必须是文件中的第一个节点 [英] Error: The XML declaration must be the first node in the document

查看:1654
本文介绍了错误:XML声明必须是文件中的第一个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到意外的XML声明。XML声明必须是文件中的第一个节点,并没有空白字符被允许在它之前出现试图加载XML错误。既我的C#code和XML文件的内容如下。在XML文件中,因此误差的6代线XML定义存在。

我无法控制那里的东西在XML文件中,这样我怎么可以编辑/使用C#这样的XML声明是第一位的,然后评论加载它没有任何错误改写了!

  // xmlFilepath是传递给这个函数的XML文件的路径/名称
静态函数(字符串xmlFilepath)
{
XmlReaderSettings readerSettings =新XmlReaderSettings();
readerSettings.IgnoreComments = TRUE;
readerSettings.IgnoreWhitespace =真;
XmlReader的读者= XmlReader.Create(XmlFilePath,readerSettings);
XmlDocument的XML =新的XmlDocument();
与XML.load(读卡器);
}
 

XmlDoc.xml

 <  - 客户ID:1  - >
!<  - 导入文件:XmlDoc.xml  - >
!<  - 开始时间:12年8月14日凌晨3:15  - >
!<  - 结束时间:12年8月14日上午03时18  - >

< XML版本=1.0编码=ISO-8859-1独立=是&GT?;
-----
 

解决方案

由于错误状态,XML文档的前五个字符应< XML 。没有如果,并且或者击打。你有开放的XML标签上面的注释是非法的;它们必须在XML标签内(因为评论结构本身由XML标准定义的,所以是无意义的主要XML标签以外)。

编辑:这样的事情应该能够重新安排行,因为从OP文件格式:

  VAR线=新的名单,其中,串>();

使用(VAR FILESTREAM = File.open方法(xmlFilePath,FileMode.Open,FileAccess.Read))
   使用(VAR读卡器=新的TextReader(FILESTREAM))
   {
      串线;
      而((行= reader.ReadLine())!= NULL)
         lines.Add(线);
   }

VAR I = lines.FindIndex(S => s.StartsWith(< XML));
VAR xmlLine =行[I]
lines.RemoveAt(ⅰ);
lines.Insert(0,xmlLine);

使用(VAR FILESTREAM = File.open方法(xmlFilePath,FileMode.Truncate,FileAccess.Write)
   使用(VAR作家=新的TextWriter(FILESTREAM))
   {
      的foreach(在行VAR行)
         writer.Write(线);

      writer.Flush();
   }
 

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.

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!

//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"?>
-----

解决方案

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).

EDIT: Something like this should be able to rearrange the rows, given the file format from the 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();
   } 

这篇关于错误:XML声明必须是文件中的第一个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆