'\0' in xml string

hi

i am reading an xml document from a string which i receive bytewise from the network. the string is assembled out of char like that s\0e\0n\0d\0 for "send" for example. i really don't know how the '\0' came in there, but so far they didn't bother me. but no with an xmldocument they do. if i try to load the string with either document.LoadXml(string) or document.innerXml = string then i get an exception saying "name cannot begin with '.' character, line 1, position 2", so there seems to be a '.' right where the \0 is in the string (so my assumption is that \0 stands for a '.', but i am really not that good at this character encoding stuff).
so what i tried to do was:
message.Trim(new char[] { '.' });
and
message.Trim(new char[] { '\0' });

well, in both cases no success, the string looks just the same as before. so how do i get rid of these \0 stuff


thanks
gabriel


Answer this question

'\0' in xml string

  • Nigel Stratton

    It works for me fine:

    static void PrintData(byte[] data) {
    int size = Math.Min(data.Length, 32);
    Console.WriteLine("byte[] data = new byte[{0}];", size);
    for (int i = 0; i < size; i ++) {
    Console.WriteLine("data[{0}] = {1};", i, dataIdea.ToString());
    }
    }

    public static void Main(string[] args) {
    byte[] data = new byteMusic;
    data[0] = (byte)'s';
    data[1] = 0;
    data[2] = (byte)'e';
    data[3] = 0;
    data[4] = (byte)'n';
    data[5] = 0;
    dataDevil = (byte)'d';

    System.Text.Encoding e = System.Text.Encoding.Unicode;
    string s = e.GetString(data);
    Console.WriteLine(s);
    Console.WriteLine();
    byte[] data2 = e.GetBytes(s);
    PrintData(data2);
    Console.WriteLine();
    Console.WriteLine(e.GetString(data2));
    }

    If you still has problem with it, plese post back your results.



  • mstngslly316

    my byte conversion looks like this:

    iRx = socketData.m_currentSocket.EndReceive(asyn);
    char[] chars = new char[iRx + 1];
    System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
    int charLen = d.GetChars(socketData.dataBuffer,
    0, iRx, chars, 0);
    System.String szData = new System.String(chars);


    iRx gets the number of chars sent and allocates a char array. socketData is a class with the dataBuffer member which is an array of 1 byte. using the Encoding.Unicode (UTF16) i get two times \0\0 in the char array. with my implementation i get s\0 for example (one char and one \0)

  • techvistas

    It seams that your string is in Unicode and you are reading it as ASCII.

    In Unicode (UTF-16) each char is represented by 2 bytes.

    How do you get these strings Do you convert them from byte[] yourself

    You may need read binary data with new StreamReader() providing it correct Encoding.



  • '\0' in xml string