Byte array to xml file c#




















Active 5 years, 5 months ago. Viewed 15k times. Load ms ; But got an exception like: An unhandled exception of type 'System.

XmlException' occurred in System. Line 1, position 1. Badhon Jain Badhon Jain 6 6 gold badges 20 20 silver badges 35 35 bronze badges. Add a comment. Active Oldest Votes. ReadToEnd ; Console. Read ; if! EndOfStream doc2.

That's because it is invalid XML. GetString response ; it'll decode the string for you. You can do doc2. LoadXml str ; and it'll load the doc for you. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Microsoft makes no warranties, express or implied, with respect to the information provided here. The zero-based index into the array specifying where the method can begin to write to the buffer.

The value of array is null. Length- offset. Starting with the. NET Framework 2. Create method to take advantage of new functionality. Like ReadChars , this method can be called successively to read large streams of embedded text. ReadAllBytes and sending the Byte array that that returns.

I don't know what your 'server' object is but the only thing that you can send is Bytes so a base String is going to have to be converted back to Bytes to send, so there's no point creating a base String from a Byte array in the first place.

Upvote 0 Downvote. Assuming that you want the data in a file at the other end, File. WriteAllBytes is the converse to File. Alternatively, you could put anything on top of the NetworkStream at the other end that can read from a Stream, e. XmlReader, and read the XML directly. Ok , I have writen two methods I know they both work , I have tested them before sending them over the network I get an error I think the problem is once the xml is converted to a byte array the send method converts it to a string If it does, WHY does it?

The only thing that you can actually send over the wire is Bytes so, if your Send method converts your Bytes to a String, it must then convert it back again anyway. That is at best inefficient and at worst it's breaking the whole mechanism. One thing I will suggest though. I'm not sure whether this is an issue or not but, just in case, reset the read position of your MemoryStream to beginning before loading the XML, which you can do using the Position property or the Seek method.

I forgot about the read position I will try that Thank you for all your help I wouldn't of gotten this far if it wasn't for your help. I will let you know if this works. Data at the root level is invalid , line 1 position 1 man we are so close So, you're saying that you can those two methods and end up with the same XML as you started with but sending the data over the wire in between causes issues?

In that case, I guess you need to look at the data that you're sending and the data that you're receiving and compare the two to see what the difference is. Presumably the data is being truncated somewhere but you'll first have to know what's happening before you can determine where. I'd suggest converting the data to hexadecimal in each case, so you can see each byte.



0コメント

  • 1000 / 1000