2012-04-07 1 views
0

にクライアントからそれを送って、私は2 clases持っている:XMLSERIALIZEクラスとサーバー

public class products 
{ 
    public string category; 
    public string name; 
    public double price; 
    public string desc; 
    public string version; 
    public string logoURL; 
    public string imgURL; 
    public string prod; 

    public string Category 
    { 
     set { categorie = value; } 
     get { return category; } 
    } 

と:

[Serializable()] 
public class groupProducts 
{ 
    public products[] produse; 
} 

を私はgroupProductsクラスをXMLSERIALIZEし、TCPを介してサーバからデータを送信したいですクライアントへの接続!以下のような 私が試した何か:

groupProducts gp = new groupProducts(); 
XmlSerializer xmlSel = new XmlSerializer(typeof(groupProducts)); 
TextWriter txtStream = new StreamWriter("xmlStreamFile.xml");  
xmlSel.Serialize(txtStream, gp); 
txtStream.Close(); 
try 
{ 
Stream inputStream = File.OpenRead("xmlStreamFile.xml"); 
// declaring the size of the byte array to the length of the xmlfile 
msg = new byte[inputStream.Length]; 
//storing the xml file in the byte array 
inputStream.Read(msg, 0, (int)inputStream.Length); 
//reading the byte array 
communicator[i].Send(msg); 
} 

が、それを私はクライアント側でそれをデシリアライズするとき - XMLファイルは、その中にいくつかの奇妙なデータを持っています!

あなたは何ができるか考えていますか?私は間違って何をしていますか?

+0

「奇妙なデータ」を定義します。それでも有効なXMLですか? –

+0

はい - それはまだXMLデータでしたが、その一部だけでした!私はそれを修正することができましたが、今度はデータが2回書き込まれます! – Alin

+0

私はそれが何を意味するのか分かりません。 –

答えて

0

1-あなたは、ストリームからinputStream.Lengthバイトを取得しますinputStream.Read(msg, 0, (int)inputStream.Length);読むdoes't保証でStreamWriter

2-開いている間、私はエンコーディングを使用する安全です。返された値をチェックする必要があります。

3一時ファイルは必要ありません。 Use

XmlSerializer xmlSel = new XmlSerializer(typeof(groupProducts)); 
MemoryStream m = new MemoryStream(); 
xmlSel.Serialize(m); 
communicator[i].Send(m.ToArray()); 
+0

'MemoryStream'で' using'を使用してください。 –

+0

@IliaG、これは単なる伝統です。 MemoryStreamには処分するリソースがありません。 –

関連する問題