By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

XML to byte[]

P: 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
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 750
PRR
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

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

Expert 100+
P: 750
PRR
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
Expert 100+
P: 229
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

Post your reply

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