Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XMLDoc无效-存在多个根元素。第9行,位置2_Xml_Json_Exception - Fatal编程技术网

XMLDoc无效-存在多个根元素。第9行,位置2

XMLDoc无效-存在多个根元素。第9行,位置2,xml,json,exception,Xml,Json,Exception,我正在调用一个返回xml的web服务。当我需要在json中使用xml时。我可以用web服务的其他方法来实现这一点。但是当调用web服务的特定方法并尝试对其进行解析时,我得到以下错误: “有多个根元素。第9行,位置2” 我收到的xml肯定有多个根元素,如下所示: <?xml version="1.0" encoding="UTF-8"?> <Server> <Name>Abc</Name> <URL>www.abc.com&l

我正在调用一个返回xml的web服务。当我需要在json中使用xml时。我可以用web服务的其他方法来实现这一点。但是当调用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(响应)行;它抛出异常的地方

请记住,这是我从Web服务获取的xml,而不是从文件获取的。所以使用XDocument在这里不起作用


任何建议、想法???

在xml中,应该有一个
标记,用于放置
元素

<?xml version="1.0" encoding="UTF-8"?>
<Servers>
<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>
</Servers>

Abc
www.abc.com
窗户
XYZ
www.xyz.com
Linux Ubuntu

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

我从第三方API(当然,我无法控制)获得xml作为响应。因此,我想将响应封装在一个名为
的根元素中,使其成为有效的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.
XmlReaderSettings xrs=新的XmlReaderSettings();
xrs.ConformanceLevel=ConformanceLevel.Fragment//由于存在多个根元素的问题,文档将无法通过验证,因此我们拒绝使用这些片段
字符串xmlString=“\n”;
使用(XmlReader xr=XmlReader.Create(新StringReader(响应),xrs))
{
而(xr.Read())
{
if(xr.NodeType!=XmlNodeType.XmlDeclaration)
{
开关(xr.NodeType)
{
case XmlNodeType.Element://如果nodetype是元素。
xmlString+=“”;
打破
case XmlNodeType.Text://获取每个元素内部的文本。
xmlString+=xr.Value;
打破
case XmlNodeType.EndElement://关闭元素。
xmlString+=“”;
打破
}
}
}
}
xmlString+=“”//xmlString现在有一个有效的xml字符串。所以XDocument或XmlDocument解析不会对其进行故障转移。
var doc=XDocument.Parse(xmlString);
var json=JsonConvert.SerializeXNode(doc,Newtonsoft.json.Formatting.Indented,true);
返回json;//我将其转换为json,以便客户端可以使用它。
完成了


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

如何插入该标记?它从一开始就应该在xml中,Web服务负责在我的文档中发送正确的xml文档。如果您是Web服务的所有者,这里将介绍如何从线程中的xml源执行此操作。不,这就是问题所在。web服务是第三方服务,我无法控制它。链接中建议的解决方案中的问题是,我必须先加载xml(doc.Load(xmlFilePath)),然后才能对其执行任何操作。但是这就是例外的地方。对不起,周末就在中间:)那我真的帮不了你。我认为您应该首先联系负责服务的人员,并确保他们的xml结果是正确的。祝你好运如果它有多个根级别的元素,那么它就不是XML文档。你需要和供应商谈一谈,让他们在他们这边解决问题。是的,我已经和他们谈过了,他们告诉我至少需要两个星期才能解决问题。所以我不得不想出一个解决办法。