简体   繁体   中英

“Root element is missing” exception given when trying to parse XML file

I'm trying to set up parsing for a test XML generated with ksoap2 in Android:

<?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>

But seemingly i can't parse it in any way. The exception is always that "Root element is not found" even when it goes through XML-validators like the one at w3schools. If i'm correct the contents of the body shouldn't be an issue when the problem is with root element.

The test code for parsing i try to use in C# is:

            using (StreamReader streamreader = new StreamReader(Context.Request.InputStream))
            {
                try
                {
                    XDocument xmlInput = new XDocument();
                    streamreader.BaseStream.Position = 0;
                    string tmp = streamreader.ReadToEnd();
                    var xmlreader = XmlReader.Create(streamreader.BaseStream);
                    xmlInput = XDocument.Parse(tmp);
                    xmlInput = XDocument.Load(xmlreader);
                catch (Exception e)
                { }

where the xmlInput = XDocument.Parse(tmp); does indeed parse it to a XDocument , not a navigable one, though. Then xmlInput = XDocument.Load(xmlreader); throws the exception for not having a root element. I'm completely at loss here because i managed to parse and navigate the almost same xml with XMLDocument and XDocument classes before, and i fear i made some changes i didn't notice.

Thanks in advance.

Update: Here's the string tmp as requested :

"<?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"

Update: Even with XDocument.Load(new StreamReader(Context.Request.InputStream, Encoding.UTF8)); the parsing will fail. XDocument.Load(new StreamReader(Context.Request.InputStream, Encoding.UTF8)); the parsing will fail.

I believe you've read to the end of the stream once already, you need to reset the position in the stream again. see: "Root element is missing" error but I have a root element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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