Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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
C# &引用;缺少根元素";尝试分析XML文件时出现异常_C#_Xml_Parsing_Ksoap2 - Fatal编程技术网

C# &引用;缺少根元素";尝试分析XML文件时出现异常

C# &引用;缺少根元素";尝试分析XML文件时出现异常,c#,xml,parsing,ksoap2,C#,Xml,Parsing,Ksoap2,我正在尝试为Android中使用ksoap2生成的测试XML设置解析: <?xml version="1.0" encoding="utf-8"?> <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v

我正在尝试为Android中使用ksoap2生成的测试XML设置解析:

<?xml version="1.0" encoding="utf-8"?>
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <v:SOAPBODY>
            <v:INFO i:type="v:INFO">
                <v:LAITETUNNUS i:type="d:string">EI_TUNNUSTA</v:LAITETUNNUS>
            </v:INFO>
            <v:TOIMINNOT i:type="v:TOIMINNOT">
                <v:TOIMINTA i:type="d:string">ASETUKSET_HAKU</v:TOIMINTA>
            </v:TOIMINNOT>
            <v:SISALTO i:type="v:SISALTO">
                <v:KUVA i:type="d:string">AGFAFDGFDGFG</v:KUVA>
                <v:MITTAUS i:type="d:string">12,42,12,4,53,12</v:MITTAUS>
            </v:SISALTO>
        </v:SOAPBODY>
    </v:Body>
</v:Envelope>
其中
xmlInput=XDocument.Parse(tmp)
确实将其解析为
XDocument
,但不是可导航的文档。然后
xmlInput=XDocument.Load(xmlreader)引发没有根元素的异常。我在这里完全不知所措,因为我以前用
XMLDocument
XDocument
类解析和导航了几乎相同的xml,我担心我做了一些没有注意到的更改

提前谢谢

更新:以下是所请求的字符串tmp:

"<?xml version=\"1.0\" encoding=\"utf-8\"?><v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"><v:Header /><v:Body><v:SOAPBODY><v:INFO i:type=\"v:INFO\"><v:LAITETUNNUS i:type=\"d:string\">EI_TUNNUSTA</v:LAITETUNNUS></v:INFO><v:TOIMINNOT i:type=\"v:TOIMINNOT\"><v:TOIMINTA i:type=\"d:string\">ASETUKSET_HAKU</v:TOIMINTA></v:TOIMINNOT><v:SISALTO i:type=\"v:SISALTO\"><v:KUVA i:type=\"d:string\">AGFAFDGFDGFG</v:KUVA><v:MITTAUS i:type=\"d:string\">12,42,12,4,53,12</v:MITTAUS></v:SISALTO></v:SOAPBODY></v:Body></v:Envelope>\r\n"
“EI_TUNNUSTAASETUKSET_Hakuagfafdgfg12,42,12,4,53,12\r\n”

更新:即使使用
XDocument.Load(新的StreamReader(Context.Request.InputStream,Encoding.UTF8));解析将失败。

我相信您已经读到了流的末尾,您需要再次重置流中的位置。请参阅:

我认为这里不需要
StreamReader
。只需使用
XDocument
直接解析xml对我来说很好(.Net 4.5)。可能是流是用错误的编码读取的吗?你能在阅读流之后粘贴
tmp
的值吗?你的样本是。。。。奇怪。网络流通常不可搜索。。。两次读取流(
ReadToEnd
XmlReader.Create
)没有多大意义。。。你确定这是你遇到问题的代码,还是你只是将多次尝试的解析按随机顺序粘贴在一起?我想这是Alexei说的。如果使用
XDocument.Load(Context.Request.InputStream)未首先将流读入字符串-错误是否仍然发生?添加了临时字符串内容。是的,代码可能看起来很愚蠢,因为它有我用来调试和试图解决这个问题的行。我还尝试了XmlReader=XmlReader.Create(Context.Request.InputStream);XDocument xmlinput=XDocument.Load(xmlReader);但它仍然无法解析。
"<?xml version=\"1.0\" encoding=\"utf-8\"?><v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"><v:Header /><v:Body><v:SOAPBODY><v:INFO i:type=\"v:INFO\"><v:LAITETUNNUS i:type=\"d:string\">EI_TUNNUSTA</v:LAITETUNNUS></v:INFO><v:TOIMINNOT i:type=\"v:TOIMINNOT\"><v:TOIMINTA i:type=\"d:string\">ASETUKSET_HAKU</v:TOIMINTA></v:TOIMINNOT><v:SISALTO i:type=\"v:SISALTO\"><v:KUVA i:type=\"d:string\">AGFAFDGFDGFG</v:KUVA><v:MITTAUS i:type=\"d:string\">12,42,12,4,53,12</v:MITTAUS></v:SISALTO></v:SOAPBODY></v:Body></v:Envelope>\r\n"