简体   繁体   English

解析xml时出错

[英]error in parsing xml

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 : url是:

http://localhost.:38734/mysubdir/data.xml 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="support@geistmfg.com" 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 我该如何解决

Try loading it with XmlDocument. 尝试使用XmlDocument加载它。 XmlDocument can understand some XMLs that XmlReader can't. XmlDocument可以理解XmlReader无法理解的一些XML。 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. 如果XmlDocument也无法理解您的XML,则您的XML组成不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM