【问题标题】:XmlDocument file name with space带空格的 XmlDocument 文件名
【发布时间】:2014-04-01 07:18:18
【问题描述】:

我在 windows7 中有一个包含服务名称的 XML 文件,其中一项服务有空格,即“服务名称”,当我加载文件时出现异常:

fileName = file;
pathToFile = path;
XmlDocument ServerList = new XmlDocument();            
ServerList.Load(pathToFile + fileName);

XML:

<systems>
 <Groups>
  <Myervices>    
     <Dialogic/>
     <BoardServer/>
     <HmpElements/>
     <Service-1 Agent/>
   </Myervices>
 </Groups>
</systems>

filenName 有空格,有没有办法接收它,因为我无法更改服务名称。

我得到的异常:

'/' 是一个意外的标记。预期的标记是“=”。第 824 行, 位置 23. 在 System.Xml.XmlTextReaderImpl.Throw(Exception e) 在 System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) 在 System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(字符串 预期令牌1,字符串预期令牌2)在 System.Xml.XmlTextReaderImpl.ParseAttributes() 在 System.Xml.XmlTextReaderImpl.ParseElement() 在 System.Xml.XmlTextReaderImpl.ParseElementContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) 在 System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) 在 System.Xml.XmlLoader.Load(XmlDocument 文档,XmlReader 阅读器,布尔值 preserveWhitespace)在 System.Xml.XmlDocument.Load(XmlReader 阅读器) 在 System.Xml.XmlDocument.Load(字符串文件名) Stop_Start_systems.Functions..ctor(String path, String file) in c:\Stop_Start_systems\Functions.cs:第 32 行 Stop_Start_systems.Default.Page_Load(Object sender, EventArgs e) in c:\Stop_Start_systems\Default.aspx.cs:第 31 行 System.Collections.ListDictionaryInterna 谢谢

【问题讨论】:

  • 那么什么你得到了什么异常?
  • 我添加行谢谢
  • 那么听起来您已经构建了一个损坏的 XML 文件。我们不知道该文件是什么样子,因此很难提出修复建议。
  • 换句话说,文件名中的空格与问题无关。只是您手动编辑了 XML,include 文件名,并且您已破坏它!
  • @ishigh:不,文件不是好。那是无效的 XML。 XML 元素名称中不能有空格。就这么简单。我建议对所有服务使用相同的元素名称,并将服务名称作为属性值。

标签: c# xml service xmldocument


【解决方案1】:

问题与 XML 文件的名称或您发布的代码无关。它与 XML 无效有关。 XML 元素名称不能包含空格,因此这是无效的:

<Service-1 Agent/>

相反,您应该为所有服务使用相同的元素名称,而是将服务名称放入属性中,例如

<Service Name="Service-1 Agent" />
<Service Name="Some other service" />

等等。我强烈建议您使用 API 自动创建 XML 文件,而不是手动创建 - 这样您更多更有可能最终得到有效的 XML。

【讨论】:

  • 有帮助 - 谢谢
猜你喜欢
  • 1970-01-01
  • 2010-12-07
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 2023-03-22
相关资源
最近更新 更多