ข้อผิดพลาดค่าเลขฐานสิบหก 0x00 เป็นอักขระที่ไม่ถูกต้องขณะส่ง DataTable ในรูปแบบ XML ผ่าน LAN

สวัสดี ฉันค่อนข้างใหม่กับการเขียนโปรแกรม NET ฉันต้องพัฒนาแอปพลิเคชันที่ไคลเอนต์ต้องการเข้าถึงเซิร์ฟเวอร์ผ่าน LAN และรับข้อมูลจากฐานข้อมูล MS ACCESS ระยะไกล การสื่อสารสำเร็จ และเซิร์ฟเวอร์กำลังส่งตารางข้อมูลในรูปแบบ XML ไม่ว่าเมื่อไคลเอนต์ได้รับสตริงและพยายามแปลงเป็น XML และกรอก datagridview ก็ทำให้เกิดข้อผิดพลาด

"ค่าเลขฐานสิบหก 0x00 เป็นอักขระที่ไม่ถูกต้อง"

ความช่วยเหลือในการแก้ไขปัญหานี้จะได้รับการชื่นชม ฉันใช้ทั้งการเข้ารหัส ASCII และการเข้ารหัส UTF-8 แต่ทั้งสองใช้ไม่ได้

นี่คือลูกค้าที่ส่งคำขอ

    Dim client As New Net.Sockets.TcpClient
    Dim stream As NetworkStream = Nothing
    sql = "Select * from Roll "

    client.Connect("127.0.0.1", 3000)
    stream = client.GetStream
    Dim sendbytes() As Byte = Encoding.ASCII.GetBytes(sql)
    stream.Write(sendbytes, 0, sendbytes.Length)

จากนั้นเซิร์ฟเวอร์จะได้รับและเรียกใช้แบบสอบถามบนฐานข้อมูลและส่ง DataTable ผ่านการเชื่อมต่อ LAN

        client = server.AcceptTcpClient
        stream = client.GetStream
        Dim rvcBytes(client.ReceiveBufferSize) As Byte
        stream.Read(rvcBytes, 0, client.ReceiveBufferSize)
        Dim recive As String = Encoding.ASCII.GetString(rvcBytes)


    Try 'lotNumberFrom & " and LotNumber " & lotNumberTo


        cmd = New OleDbCommand(recive, con)
        '  MsgBox(recive)
        ds.Clear()

        adapter.SelectCommand = cmd
        adapter.Fill(ds, "Batch data")

        Dim writer As New System.IO.StringWriter
        ds.Tables("Batch data").WriteXml(writer, True)

        ' MsgBox(writer.ToString)
        Dim sendbytes() As Byte = Encoding.ASCII.GetBytes(writer.ToString)
        stream.Write(sendbytes, 0, sendbytes.Length)





    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

จากนั้นไคลเอนต์ได้รับมันและพยายามเติม datagridview นั่นคือสิ่งที่ปัญหาเกิดขึ้น

    Dim rvcBytes(client.ReceiveBufferSize) As Byte
    stream.Read(rvcBytes, 0, client.ReceiveBufferSize)
    Dim recive As String = Encoding.ASCII.GetString(rvcBytes)
    Dim dsa As New DataSet



    dsa.ReadXml(New XmlTextReader(New StringReader(recive)))
    DataGridView1.DataSource = dsa.Tables(0)

    client.Close()

person SameeraR    schedule 16.05.2013    source แหล่งที่มา
comment
คำถามของคุณยังขาดอยู่มาก - เราไม่รู้เลยว่าคุณถ่ายโอนข้อมูลอย่างไร และคุณได้รับข้อมูลอย่างไร เริ่มต้นด้วยการให้รหัสนั้น   -  person Jesse    schedule 16.05.2013
comment
ขออภัยจริงๆ ฉันแก้ไขคำถามแล้ว   -  person SameeraR    schedule 16.05.2013


คำตอบ (1)


ฉันเริ่มต้นด้วยการพยายามปรับแต่งโค้ดของคุณ แต่ในขณะที่ค้นหาข้อมูล ฉันพบตัวอย่าง MSDN สองสามตัวอย่าง:

ตัวอย่างซ็อกเก็ตเซิร์ฟเวอร์แบบซิงโครนัส
http://msdn.microsoft.com/en-CA/library/6y0e13d3.aspx

ตัวอย่างซ็อกเก็ตไคลเอ็นต์แบบซิงโครนัส
http://msdn.microsoft.com/en-CA/library/kb5kfec7.aspx

จากการยืมเงินจำนวนมากจากสิ่งเหล่านั้น ฉันจึงได้สิ่งต่อไปนี้ (อยู่ในภาษา C# แต่การแปลเป็น VB.NET ก็ไม่น่าจะยากนัก)

ACE_Server.exe เป็นแอปพลิเคชันคอนโซล มันจะคอยฟังการเชื่อมต่อจนกว่าคุณจะฆ่ามันด้วย Ctrl+C:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketListener
{
    // Incoming data from the client.
    public static string data = null;

    public static void StartListening()
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        IPHostEntry ipHostEntry = Dns.GetHostEntry("localhost");
        IPAddress ipAddress = ipHostEntry.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and 
        // listen for incoming connections.
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);

            // Start listening for connections.
            while (true)
            {
                Console.WriteLine("Waiting for a connection...");
                // Program is suspended while waiting for an incoming connection.
                Socket handler = listener.Accept();
                data = null;

                // An incoming connection needs to be processed.
                while (true)
                {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    if (data.IndexOf("<EOF>") > -1)
                    {
                        break;
                    }
                }

                // Show the data on the console.
                Console.WriteLine("Text received : {0}", data);

                // do the database stuff
                string sql = data.Substring(0, data.Length - 5);  // remove "<EOF>" terminator
                var con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\__tmp\testData.accdb;");
                var cmd = new System.Data.OleDb.OleDbCommand(sql, con);
                var adapter = new System.Data.OleDb.OleDbDataAdapter();
                adapter.SelectCommand = cmd;
                var ds = new System.Data.DataSet();
                adapter.Fill(ds, "Batch data");

                var sw = new System.IO.StringWriter();
                ds.Tables["Batch data"].WriteXml(sw, System.Data.XmlWriteMode.IgnoreSchema);

                // send the data back to the client.
                byte[] msg = Encoding.ASCII.GetBytes(sw.ToString());
                int bytesSent = handler.Send(msg);
                Console.WriteLine(bytesSent.ToString() + " bytes sent.");
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }

    public static int Main(String[] args)
    {
        StartListening();
        return 0;
    }
}

ACE_Client.exe เป็นแอปพลิเคชัน Windows Forms โปรดทราบว่าจะเติม "<EOF>" ต่อท้ายสตริง SQL เพื่อให้ส่วนประกอบเซิร์ฟเวอร์รู้ว่าเมื่อใดควรหยุดอ่าน:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace ACE_Client
{
    public partial class Form1 : Form
    {
        public static string data = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            // Establish the remote endpoint for the socket.
            // This example uses port 11000 on the local computer.
            IPHostEntry ipHostEntry = Dns.GetHostEntry("localhost");
            IPAddress ipAddress = ipHostEntry.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

            // Create a TCP/IP  socket.
            Socket sock = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
            sock.Connect(remoteEP);

            string sql = "SELECT * FROM ExpenseDetails";
            byte[] msg = Encoding.ASCII.GetBytes(sql + "<EOF>");

            // Send the data through the socket.
            int bytesSent = sock.Send(msg);

            // Receive the response from the remote device.
            data = null;
            while (true)
            {
                bytes = new byte[1024];
                int bytesRec = sock.Receive(bytes);
                data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                if (data.IndexOf("</NewDataSet>") > -1)
                {
                    break;
                }
            }

            // Release the socket.
            sock.Shutdown(SocketShutdown.Both);
            sock.Close();

            var dsa = new DataSet();
            dsa.ReadXml(new System.Xml.XmlTextReader(new System.IO.StringReader(data)));
            dataGridView1.DataSource = dsa.Tables[0];
        }
    }
}
person Gord Thompson    schedule 16.05.2013
comment
รหัสของคุณใช้งานได้อย่างมีเสน่ห์ ไม่รู้จะขอบคุณยังไง.. ในที่สุดมันก็ใช้งานได้ ขอบคุณมากที่สละเวลากับปัญหานี้ ไชโย.... - person SameeraR; 17.05.2013
comment
คุณช่วยอธิบายสิ่งที่ผิดปกติกับรหัสเริ่มต้นได้ไหม - person guival; 09.02.2017