xml - 无效的 XMLDoc - 有多个根元素。 9号线,位置2

标签 xml json exception

我正在调用返回 xml 的 Web 服务。当我需要将 xml 保存为 json 供以后使用时。我可以使用网络服务的其他方法来做到这一点。但是当调用 Web 服务的特定方法并尝试解析它时,我收到以下错误:

“有多个根元素。第 9 行,位置 2”。

我收到的 xml 肯定有多个根元素,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Server>
   <Name>Abc</Name>
   <URL>www.abc.com</URL>
   <Env>Windows</Env>
</Server>
<Server>
   <Name>XYZ</Name>
   <URL>www.xyz.com</URL>
   <Env>Linux-Ubuntu</Env>
</Server>

所以基本上当我这样做时:

XmlDocument doc = new XmlDocument();
doc.LoadXml(response); // response contains the xml shown above
if (doc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
        doc.RemoveChild(doc.FirstChild); // Because I want to get rid of the declaration element in the xml.

var resXML = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented, true); // To convert xml into json

return resXML; // Finally return the json.

调试时,是doc.LoadXml(response);这一行抛出异常的地方。

请记住,这是我从网络服务获取的 xml,而不是从文件获取的。因此,使用 XDocument 在这里不起作用。

有什么建议、想法吗?

最佳答案

好吧,我自己解决了。但当然,我从另一个论坛得到的帮助很少,无法了解如何做到这一点。 所以场景实际上是这样的:

我从第三方 API 获取了 xml 作为响应(当然我对此没有任何控制权)。所以我想将响应封装在名为 <Servers></Servers> 的根元素中所以它成为有效的 xml,然后我可以使用 XmlDocument 或 XDocument 进行解析。

为了解决这个问题,我使用了以下逻辑将其封装在根元素内。

XmlReaderSettings xrs = new XmlReaderSettings();
        xrs.ConformanceLevel = ConformanceLevel.Fragment; //We confrom to the fragments because the document will not pass validation due to multiple root elements problem
        String xmlString = "<Servers>\n";
        using (XmlReader xr = XmlReader.Create(new StringReader(response), xrs))
        {
            while (xr.Read())
            {
                if (xr.NodeType != XmlNodeType.XmlDeclaration)
                {
                    switch (xr.NodeType)
                    {
                        case XmlNodeType.Element: // If nodetype is an element.
                            xmlString += "<" + xr.Name + ">";
                            break;
                        case XmlNodeType.Text: //Get text inside each element.
                            xmlString += xr.Value;
                            break;
                        case XmlNodeType.EndElement: //Close the element.
                            xmlString += "</" + xr.Name + ">";
                            break;
                    }
                }
            }
        }
        xmlString += "</Servers>"; //xmlString now has a string which is a valid xml. So XDocument or XmlDocument parse will not fail over it.

        var doc = XDocument.Parse(xmlString);
        var json = JsonConvert.SerializeXNode(doc,Newtonsoft.Json.Formatting.Indented, true);
        return json; // I convert it to json so the client can consume it.

完成!

当然,这是一种解决方法,但由于 API 人员需要一些时间才能修复无效的 xml,所以在此之前我必须这样做。

关于xml - 无效的 XMLDoc - 有多个根元素。 9号线,位置2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29564010/

相关文章:

exception - hudson |加载作业时出现 NullPointerException

c# - .Net 中不支持的类型异常

ios - NSUserDefaults 有时会保存详细信息,有时则不会

python - XSL转换,请求子节点数据

c# - 在xml文件中搜索数据c#

java - 无法打开类路径资源 [pointsconfig.properties],因为它不存在

android - 具有折叠工具栏和 ImageView 的视差 TabLayout

php - 将 Propel 转换为带有链接实体的 JSON

json - ReactJS - 从 URL 获取 json 对象数据

javascript - 如何在本地javascript中运行jsonPath?