解析xml时出错
我有以下代码片段:
var settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
reader = XmlReader.Create(url, settings);
try
{
doc.Load(reader); // Error here
}
catch(Exception ex)
{
}
其中 url 是:
http://localhost.:38734/mysubdir/data.xml
但我收到错误:
System.Xml.XmlException: Reference to undeclared entity 'nbsp'. Line 111, position 20.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.HandleGeneralEntityReference(String name, Boolean isInAttributeValue, Boolean pushFakeEntityIfNullResolver, Int32 entityStartLinePos)
at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at EnviroTracker.Feed.FeedReader.Get(String url, String username, String password) in c:\FeedReader.cs:line
<?xml version="1.0" encoding="UTF-8"?>
<server host="IPC_Server_Room" address="10.10.50.22" name="RSM" owl-version="CB_705" product-version="3.5.0" pversion="2.00" mac-address="00:19:85:E0:20:DF" datetime="Tue, 03/01/11 23:53:01" company="Geist Manufacturing" company-url="http://www.abc.com" support-email="[email protected]" support-phone="800.432.3219" console-id="chh" tempunit="F" uptime="238871">
<cameras>
</cameras>
<devices>
<device id="0145B5D613000098" name="IPC_Server_Room" type="GeistGoos" available="1" index="0">
<field key="TempC" value="24.86" niceName="Temperature (C)" min="-40.0" max="123.8" type="0" />
<field key="TempF" value="76.74" niceName="Temperature (F)" min="-40.0" max="254.8" type="2" />
<field key="Humidity" value="32" niceName="Relative Humidity" min="0.0" max="100.0" type="2" />
<field key="DewPtC" value="7.0" niceName="Dew Point (C)" min="-40.0" max="123.8" type="0" />
<field key="DewPtF" value="44.7" niceName="Dew Point (F)" min="-40.0" max="254.8" type="2" />
<field key="Light" value="3" niceName="Light Level" min="1.0" max="100.0" type="2" />
<field key="Airflow" value="23" niceName="Air Flow" min="0.0" max="100.0" type="2" />
<field key="Sound" value="3" niceName="Sound Level" min="0.0" max="99.0" type="2" />
<field key="IO1" value="98" niceName="Power" min="0.0" max="99.0" type="2" />
<field key="IO2" value="97" niceName="Door Contact" min="0.0" max="99.0" type="2" />
<field key="IO3" value="99" niceName="Water Snake" min="0.0" max="99.0" type="2" />
<field key="Volts" value="122.8" niceName="Volts" min="0.0" max="275.0" type="2" />
<field key="Volt-Pk" value="122.9" niceName="Volts (Peak)" min="0.0" max="275.0" type="2" />
</device>
</devices>
<alarms>
</alarms>
</server>
我该如何修复它
I have following code snippet:
var settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
reader = XmlReader.Create(url, settings);
try
{
doc.Load(reader); // Error here
}
catch(Exception ex)
{
}
where url is :
http://localhost.:38734/mysubdir/data.xml
But I get error :
System.Xml.XmlException: Reference to undeclared entity 'nbsp'. Line 111, position 20.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.HandleGeneralEntityReference(String name, Boolean isInAttributeValue, Boolean pushFakeEntityIfNullResolver, Int32 entityStartLinePos)
at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at EnviroTracker.Feed.FeedReader.Get(String url, String username, String password) in c:\FeedReader.cs:line
<?xml version="1.0" encoding="UTF-8"?>
<server host="IPC_Server_Room" address="10.10.50.22" name="RSM" owl-version="CB_705" product-version="3.5.0" pversion="2.00" mac-address="00:19:85:E0:20:DF" datetime="Tue, 03/01/11 23:53:01" company="Geist Manufacturing" company-url="http://www.abc.com" support-email="[email protected]" support-phone="800.432.3219" console-id="chh" tempunit="F" uptime="238871">
<cameras>
</cameras>
<devices>
<device id="0145B5D613000098" name="IPC_Server_Room" type="GeistGoos" available="1" index="0">
<field key="TempC" value="24.86" niceName="Temperature (C)" min="-40.0" max="123.8" type="0" />
<field key="TempF" value="76.74" niceName="Temperature (F)" min="-40.0" max="254.8" type="2" />
<field key="Humidity" value="32" niceName="Relative Humidity" min="0.0" max="100.0" type="2" />
<field key="DewPtC" value="7.0" niceName="Dew Point (C)" min="-40.0" max="123.8" type="0" />
<field key="DewPtF" value="44.7" niceName="Dew Point (F)" min="-40.0" max="254.8" type="2" />
<field key="Light" value="3" niceName="Light Level" min="1.0" max="100.0" type="2" />
<field key="Airflow" value="23" niceName="Air Flow" min="0.0" max="100.0" type="2" />
<field key="Sound" value="3" niceName="Sound Level" min="0.0" max="99.0" type="2" />
<field key="IO1" value="98" niceName="Power" min="0.0" max="99.0" type="2" />
<field key="IO2" value="97" niceName="Door Contact" min="0.0" max="99.0" type="2" />
<field key="IO3" value="99" niceName="Water Snake" min="0.0" max="99.0" type="2" />
<field key="Volts" value="122.8" niceName="Volts" min="0.0" max="275.0" type="2" />
<field key="Volt-Pk" value="122.9" niceName="Volts (Peak)" min="0.0" max="275.0" type="2" />
</device>
</devices>
<alarms>
</alarms>
</server>
How can I fix it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 XmlDocument 加载它。 XmlDocument 可以理解一些 XmlReader 不能理解的 XML。不知道为什么会这样,但这是我的实践的事实。如果 XmlDocument 也不理解您的 XML,则您的 XML 的组成不正确。
Try loading it with XmlDocument. XmlDocument can understand some XMLs that XmlReader can't. Don't know why this is so, but this is a fact from my practice. If XmlDocument also doesn't understand your XML, then your XML is composed incorrectly.