4

Hello guys I have a problem when I need load a xml

My code is like to

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

The xml file is

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

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

I found something interesting

When I save the file, instead of keeping it in the Stream, perfectly loaded

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

it appears that the error only occurs in the Stream, Why?

This method convert xml in String to a MemoryStream

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;
        }
jasilva
  • 730
  • 3
  • 17
  • 45
  • I don't see any multiple root elements. Why are you throwing that exception BTW? – Rahul Sep 07 '15 at 17:51
  • I can't reproduce your exception. See https://dotnetfiddle.net/vgMXob – dbc Sep 07 '15 at 18:32
  • Where does that `Stream` come from? How are you creating it? – dbc Sep 07 '15 at 19:54
  • @dbc this is very rare error, I edit the post, it seems that if you load from a String works perfect, as shown in the test – jasilva Sep 07 '15 at 19:55
  • @jasilva - I read your most recent edit before posting my comment. That's why I was asking about the provenance of the `Stream`. – dbc Sep 07 '15 at 19:56
  • @dbc I posted new, edit before seeing your comment of the stream – jasilva Sep 07 '15 at 19:57
  • Try using the Utf8 encoding explicitly and disable (or enable) the BOM when writing to your `MemoryStream`: see http://stackoverflow.com/questions/2437666/write-text-files-without-byte-order-mark-bom – dbc Sep 07 '15 at 20:00
  • My version of `GenerateStreamFromString` does the following: `return new MemoryStream(Encoding.Unicode.GetBytes(s ?? ""));` – dbc Sep 07 '15 at 20:02
  • Still can't reproduce your problem. See https://dotnetfiddle.net/vgMXob (updated). – dbc Sep 07 '15 at 20:07
  • @dbc Utf8 in StringWriter generates the same error – jasilva Sep 07 '15 at 20:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89085/discussion-between-jasilva-and-dbc). – jasilva Sep 08 '15 at 16:03

1 Answers1

0

Since you're reading from a stream, I would assume it's not positioned at the beginning when you call xmlDoc.Load(Stream). What are you doing with the stream before the call?

If your stream supports seeking, you could try moving back to its beginning just before the call:

Stream.Seek(0, SeekOrigin.Begin);
xmlDoc.Load(Stream);
Damir Arh
  • 17,637
  • 2
  • 45
  • 83