473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

XML to byte[]

10
Hi,

I'm writing XML simulator that suppose to be a black box.
Loads xml messages, transforms them into byte[] and sends to the other application via TCP/IP protocol without knowing how the application on the other side is implemented.

I need to convert XML message to byte[] and send it via TCP/IP,
but I can't use serialization, because nobody will do the desirialize on the other side(TCP/IP).
Is there any other way?
I have the xsd.

Thanks
Jan 9 '09 #1
4 27031
PRR
750 Expert 512MB
You could read all the contents of xml file into string and then convert to bytes...
Expand|Select|Wrap|Line Numbers
  1. public void XMLtoByte()
  2. {
  3. FileStream fs = File.OpenRead(@"c:\fname.XML");
  4.             byte[] bytes = ReadWholeArray(fs);
  5.  
  6.  
  7.             StreamWriter sw = new StreamWriter(@"C:\fnew.XML");
  8.             string write = System.Text.Encoding.Default.GetString(bytes);
  9.             sw.Write(write);
  10.             sw.Close();
  11. }
  12.  
  13.  
  14. public static byte[] ReadWholeArray(Stream stream)
  15.         {
  16. //Source
  17. //http://www.yoda.arachsys.com/csharp/readbinary.html
  18. //Jon Skeet
  19.             byte[] data=new byte[stream.Length];
  20.  
  21.             int offset = 0;
  22.             int remaining = data.Length;
  23.             while (remaining > 0)
  24.             {
  25.                 int read = stream.Read(data, offset, remaining);
  26.                 if (read <= 0)
  27.                     throw new EndOfStreamException
  28.                         (String.Format("End of stream reached with {0} bytes left to read", remaining));
  29.                 remaining -= read;
  30.                 offset += read;
  31.             }
  32.  
  33.             return data;
  34.         }
  35.  
Or you could use....
Expand|Select|Wrap|Line Numbers
  1. string fileContents = File.ReadAllText(@"c:\filename.XML");
  2. byte[] bytes = System.Text.Encoding.UTF8.GetBytes(fileContents);
  3.  string send = Convert.ToBase64String(bytes);
  4.  
for sending over a network...as specifying encoding is better...
Jan 9 '09 #2
maya7
10
Thanks for the suggestion, but it is not exactly the result I'm looking for.
I probably didn't explain myself correctly.

xml example:

<header>
<id>6368</id>
<srcAddr>127.0.0.1</srcAddr>
</header>

xsd example:

<xs: element name= "header">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="srcAddr" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs: element name= "header">

I need to create buffer, that will hold ( according to above example )
int(id), string(srcAddr) and not the string xml.

The question is, how can I get the element type, while parsing the xml in order to convert the element value and write it to buffer?

I'm trying to find the solution very long time, I'll appreciate your help.

Thanks,
Adi
Jan 9 '09 #3
PRR
750 Expert 512MB
To me serialization looks straight forward... You could sent byte[] to destination and make a xml file of it... Read the xml file and serialize it to object?
Other way would be XmlReaderSettings Class (System.Xml)
Look into
System.Xml.Schema
XmlReader;
Jan 12 '09 #4
vekipeki
229 Expert 100+
Add a class similar to this:

Expand|Select|Wrap|Line Numbers
  1. /*
  2. using System.Xml;
  3. using System.Xml.Serialization;
  4. */
  5.  
  6. [XmlType("header")]
  7. public class Header
  8. {
  9.     private string _id;
  10.     [XmlAttribute("id")]
  11.     public string ID
  12.     {
  13.         get { return _id; }
  14.         set { _id = value; }
  15.     }
  16.  
  17.     private string _sourceAddress;
  18.     [XmlAttribute("srcAddr")]
  19.     public string SourceAddress
  20.     {
  21.         get { return _sourceAddress; }
  22.         set { _sourceAddress = value; }
  23.     }
  24. }
Then you can deserialize it:

Expand|Select|Wrap|Line Numbers
  1. Header h = XmlHelper<Header>.Deserialize(@"SomeFile.xml");
I used the XmlHelper class from another coding blog: Let me try how this posting business works.

After the deserialization you can use h.ID and h.SourceAddress to create your byte[] array.
Jan 12 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.