Pregunta

I am trying to move and XML file from a folder in a server to another server. when the xml it's moved it appears with nulls between the letters. (Example: <[null]F[null]o[...]r[...]m[...]\n> != '<'form'\'>) ('quotes for clarification')

This is my scenario. I have a WebService that transfer XML files to my folder "received" in my server SA. I also have a windows service in another server that check if new files goes to Folder "received", this service move the files from Folder A in server SA to Folder "Processed" in server SA. In order to move the file to the folder "Processed", this Service has to transfer that xml to folder C in the server SB, using a WCF service.

The files in the server SA doesn't have the nulls between the letters, but I dunno why it have it in my SB server.

This is the code I'm using to move the files.

Dim fs As New FileStream(Path.Combine(sourcepath, NombreEncuesta), FileMode.Open)
Dim archivo(fs.Length) As Byte
fs.Read(archivo, 0, archivo.Length)
sb.FileSend = archivo
fs.Close()
¿Fue útil?

Solución

I solved my problem. The problem wasn't opening and reading the bytes in the server. The problem was in the source of the file (before going to the Folder Recieved).

The problem when I was writing the bytes of the file, I was reading and writhing the bytes of the file using This method and then moving the file to the folder "Received". That method gives me an empty byte for each byte the string has.

Byte[] MyBytes = GetBytes(MyDataSet.GetXml())

// MyBytes == {[F], 0 , [O] , 0 , [R] , 0 , [M]}

Maybe I got that answer for the reason Tim S. Said. The GetBytes Method could be reading the string in a different enconding.

The method MyDataSet.GetXml() gives you the Xml Text without the enconding {<}?xml version="1.0" encoding="utf-8" ?> (removing brakets). So when I decided to remove the zero bytes, I couldn't read them using the DataSet.ReadXml ()

When I found out thoses errors, I decided to change the way I was ureading the Bytes of the file. Instead of using the GetBytes(MyDataSet.GetXml()), I used this function.

public byte[] GetBytesFromFile(string FilePath)
    {
        FileStream Reader = null;;
        byte[] bts = null ;
        try
        {
            Reader = new FileStream(FilePath, FileMode.Open);
            bts = new byte[Reader.Length];
            Reader.Read(bts, 0, (int)Reader.Length);
            Reader.Close();
        }
        catch (Exception ex)
        {
            bts = null;
        }
        finally
        {
            if (Reader != null)
                Reader.Close();
        }
        return bts;
    }

Why I decided not to use the GetXml() Method? Because when I read the resulting bytes, I was writhing an XML without the Encoding.

Thanks Tim S. for the Ideas.

This MSDN forum helped me a Lot too

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top