【发布时间】:2012-07-25 23:58:28
【问题描述】:
我需要一些帮助来阅读格式奇怪的 XML 文件。由于节点和属性的结构方式,我不断遇到 XMLException 错误(至少,这是输出窗口告诉我的;我的断点拒绝触发以便我可以检查它)。无论如何,这是 XML。有没有人经历过这样的事情?
<ApplicationMonitoring>
<MonitoredApps>
<Application>
<function1 listenPort="5000"/>
</Application>
<Application>
<function2 listenPort="6000"/>
</Application>
</MonitoredApps>
<MIBs>
<site1 location="test.mib"/>
</MIBs>
<Community value="public"/>
<proxyAgent listenPort="161" timeOut="2"/>
</ApplicationMonitoring>
干杯
编辑:解析代码的当前版本(文件路径缩短 - 我实际上并没有使用这个):
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"..\..\..\ApplicationMonitoring.xml");
string port = xml.DocumentElement["proxyAgent"].InnerText;
【问题讨论】:
-
<ApplicationMonitoring>似乎没有结束标签 -
如果这是整个文档,那是非法的 XML,因为
ApplicationMonitoring根元素永远不会关闭,这可能解释了您的问题。这就是您要解析的全部内容吗? -
它不是有效的 XML,因为没有关闭
<ApplicationMonitoring>标记。 -
不,这只是我的一个糟糕的副本。它有一个结束标签。
-
@Skulmuk:那么你应该确保它存在于问题中......