XmlDocument.Load multiple root elements

Hi guys, I have a problem when I need to download xml

My code is like

Stream.Seek(0, SeekOrigin.Begin);
xmlDoc.Load(Stream); //--> throw XmlException (multiple root elements in 14,22)
xmlDoc.PreserveWhitespace = true;

Xml file

<soapenv:Envelope xmlns:dummy="

http://www.somedomian.com/dummybus/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header Id="IDH">
        <dummy:authentication>
            <id>Unique</id>
            <userid>myuser</userid>
        </dummy:authentication>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsu:Timestamp>
                <wsu:Created>2015-09-07T12:21:15</wsu:Created>
                <wsu:Expires>2015-09-08T12:21:15</wsu:Expires>
            </wsu:Timestamp>
        </wsse:Security>
    </soapenv:Header> <!--- This is line 14 and  > is a column 22--->
    <soapenv:Body>
        <dummy:dummybus>
            <msg>X1</msg>
        </dummy:dummybus>
    </soapenv:Body>
</soapenv:Envelope>
Tag

<Envelope>is the root of the file. Why send this error and how can I read the file?

I found something interesting

When I save a file, and not save it to a stream, completely loaded

xmlDoc.Load(urlToFile);
xmlDoc.PreserveWhitespace = true;

it seems that the error only occurs in the stream, why?

This method converts xml StringtoMemoryStream

public class XmlUtil
    {
        public static MemoryStream GenerateStreamFromString(string s)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }
+4
source share
1 answer

Since you are reading from a stream, I would suggest that it is not located at the beginning when you call xmlDoc.Load(Stream). What do you do with the thread before the call?

, :

Stream.Seek(0, SeekOrigin.Begin);
xmlDoc.Load(Stream);
0

Source: https://habr.com/ru/post/1606298/


All Articles