【发布时间】: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