0

I have a Socket communication in my Windows Phone app, but when I call it I can sometimes get a result, that looks like this

\0\0\0\0<RESULT><DATA>...</DATA></RESULT>\0 

I want to remove the start and the end of it so I only get the XML, but what is the best way to do it? I have thought about Regex, but I can not make it work :(

The87Boy
  • 877
  • 4
  • 14
  • 32
  • 2
    What are you talking to? It sounds like quite possibly you've got the protocol slightly wrong. – Jon Skeet Aug 08 '12 at 10:01
  • 2
    You could use `string xml = ReturnString.Trim('\0');` I guess. – yogi Aug 08 '12 at 10:03
  • @NikhilAgrawal I have tried many things, but right now I am trying with 2 regexes, where they are almost equal and looks like this Regex.Replace(DataFromServer, "^.*?<", "<"); – The87Boy Aug 08 '12 at 10:03
  • @JonSkeet The communication is working right, after I added header and the end-byte to the message – The87Boy Aug 08 '12 at 10:04
  • @yogi That is tried, but I do not work in the start, as there is some other letters, I can not copy-paste here (I do not know why, but Visual Studio shows them as question marks) – The87Boy Aug 08 '12 at 10:06
  • Are you somehow misinterpreting a byte-order-mark (http://stackoverflow.com/questions/1772321/what-is-xml-bom-and-how-do-i-detect-it) ? – Brian Agnew Aug 08 '12 at 10:06
  • @yogi My Socket looks like this new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); – The87Boy Aug 08 '12 at 10:07
  • @BrianAgnew It could be, but how do I remove it? – The87Boy Aug 08 '12 at 10:09
  • [This early question](http://stackoverflow.com/questions/5501284/how-do-i-remove-all-characters-in-a-string-until-a-substring-is-matched-in-ruby) might be useful. – Jaime Oro Aug 08 '12 at 10:17
  • @The87Boy: If you're garbage at the start and end of the message, I don't think you *do* have the communication working correctly. Please add more information in your question. – Jon Skeet Aug 08 '12 at 11:01
  • @JonSkeet I found out of that my client was receiving the reciept, then the total amount of bytes, then the body and at the end a end-byte, so it was integrated correct, but I could not figure out the reciept – The87Boy Sep 01 '12 at 12:57

3 Answers3

1

It sounds like a problem with your protocol, but to remove the \0 characters you could do a simple trimming of the string.

If your string has the actual \ and 0 characters in it then you could do the following:

var fixedData = receivedData.Trim(new[] { '\\', '0' });

And if the string starts with null characters (encoded as \0) then you could to:

var fixedData = receivedData.Trim(new[] { '\0' });

Both examples assume that the variable receviedData is a String containing your data.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
0

I have not enough information to answer you correctly, so I will suggest the following :

You should try to map the data you are receiving from your socket to a serializable class. And then you serialize the classObject to an XML document.

public class socketDataToMap
{
    public string Data {get; set;}
}

and then

var ser = new XmlSerializer(typeof(socketDataToMap));
using (var reader = XmlReader.Create(yourSocketStream))
            {
                var deserializedObject = ser.Deserialize(reader) as socketDataToMap;

            }

and then you serialize to an XML file. I see something like this for your problem.

h1ghfive
  • 196
  • 7
0

If your result have to be XML and more characters(else \0) can be in start or end of your string, these code can be useful:

var start = inputString.IndexOf('<');
var end = inputString.LastIndexOf('>');
var result = inputString.Substring(start, end - start);
Ria
  • 10,237
  • 3
  • 33
  • 60