XML to byte[]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maya7
    New Member
    • Dec 2008
    • 10

    XML to byte[]

    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
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    You could read all the contents of xml file into string and then convert to bytes...
    Code:
    public void XMLtoByte()
    {
    FileStream fs = File.OpenRead(@"c:\fname.XML");
                byte[] bytes = ReadWholeArray(fs);
    
    
                StreamWriter sw = new StreamWriter(@"C:\fnew.XML");
                string write = System.Text.Encoding.Default.GetString(bytes);
                sw.Write(write);
                sw.Close();
    }
    
    
    public static byte[] ReadWholeArray(Stream stream)
            {
    //Source
    //http://www.yoda.arachsys.com/csharp/readbinary.html
    //Jon Skeet
                byte[] data=new byte[stream.Length];
    
                int offset = 0;
                int remaining = data.Length;
                while (remaining > 0)
                {
                    int read = stream.Read(data, offset, remaining);
                    if (read <= 0)
                        throw new EndOfStreamException
                            (String.Format("End of stream reached with {0} bytes left to read", remaining));
                    remaining -= read;
                    offset += read;
                }
    
                return data;
            }
    Or you could use....
    Code:
    string fileContents = File.ReadAllText(@"c:\filename.XML");
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(fileContents);
     string send = Convert.ToBase64String(bytes);
    for sending over a network...as specifying encoding is better...

    Comment

    • maya7
      New Member
      • Dec 2008
      • 10

      #3
      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

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        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 XmlReaderSettin gs Class (System.Xml)
        Look into
        System.Xml.Sche ma
        XmlReader;

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          Add a class similar to this:

          Code:
          /*
          using System.Xml;
          using System.Xml.Serialization;
          */
          
          [XmlType("header")]
          public class Header
          {
              private string _id;
              [XmlAttribute("id")]
              public string ID
              {
                  get { return _id; }
                  set { _id = value; }
              }
          
              private string _sourceAddress;
              [XmlAttribute("srcAddr")]
              public string SourceAddress
              {
                  get { return _sourceAddress; }
                  set { _sourceAddress = value; }
              }
          }
          Then you can deserialize it:

          Code:
          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.

          Comment

          Working...