【问题标题】:Error: XmlException was unhandled by user code错误:用户代码未处理 XmlException
【发布时间】:2019-05-10 17:07:40
【问题描述】:

我有一个错误,例如:“System.Xml.dll 中发生‘System.Xml.XmlException’类型的异常,但未在用户代码中处理”

在我的程序中,我使用来自该站点的服务引用:http://www.oorsprong.org/websamples.countryinfo/CountryInfoService.wso,并且在运行程序时,它会阻止我使用字符串。

XML 文件:(我只想取 sName)

<ArrayOftContinent>
    <tContinent>
        <sCode>AF</sCode>
        <sName>Africa</sName>
    </tContinent>
</ArrayOftContinent>

代码如下:

org.oorsprong.www.CountryInfoService myWS3 = new org.oorsprong.www.CountryInfoService();
string str = Convert.ToString(myWS3.ListOfContinentsByName());
XmlDocument doc = new XmlDocument();
doc.LoadXml(str); /*It stops me here*/

你能帮我解决这个问题吗? 提前致谢!

【问题讨论】:

  • 那么,str 中的字符串是什么样的?
  • str 是一个 XML 字符串,它将以 xml 格式保存所有大洲 @LasseVågsætherKarlsen
  • @Christina 请复制该字符串中的实际 xml 文本,并将其粘贴到您的问题中,以便我们查看并使用它测试您的代码。
  • 您说它是一个 xml 字符串,但显然 XmlDocument 不同意,您需要粘贴您的 XML 或生成一个 minimal reproducible example 供我们尝试。
  • 那个xml是str的内容吗?您验证了这个?我怀疑的是,您实际上是从您在那里声明的那个客户端获取了一些对象的列表,并且您的 ToString 方法将其转换为类似 "System.Collections.Generic.List'1[org.oorsprong.www.Country]"

标签: c# asp.net xml xml-parsing xmldocument


【解决方案1】:

您的 wso 正在返回大陆对象,而不是 xml 字符串。阅读定义: http://www.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?op=ListOfContinentsByName

你想做这样的事情:

org.oorsprong.www.CountryInfoService myWS3 = new org.oorsprong.www.CountryInfoService();
var continents = myWS3.ListOfContinentsByName();

foreach (var continent in continents)
       Debug.WriteLine(continent.sName)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2014-10-18
    相关资源
    最近更新 更多