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

11
  • 2
    What are you talking to? It sounds like quite possibly you've got the protocol slightly wrong. Commented Aug 8, 2012 at 10:01
  • 2
    You could use string xml = ReturnString.Trim('\0'); I guess. Commented Aug 8, 2012 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, "^.*?<", "<"); Commented Aug 8, 2012 at 10:03
  • @JonSkeet The communication is working right, after I added header and the end-byte to the message Commented Aug 8, 2012 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) Commented Aug 8, 2012 at 10:06

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This does not remove all the characters :(
If you have more characters than the ones in your example code then you must add them as well in the call to .Trim.
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.

1 Comment

I can not make this work, as Windows Phone only support asyncrhonous socket-communication
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.