2017-05-10 16 views
1

あいまいな質問のお詫び。私はサードパーティのアプリケーションがリッスンしているポートにxmlメッセージを送信するC#コンソールアプリケーションを作成しようとしています。アプリケーションは別のxmlメッセージを送り返しますので、私もそれを読む必要があります。どんな提案も大歓迎です。ポートにxmlメッセージを送信

このlinkは、私が何をしようとしているかを示しています。

+0

ん[このポスト](http://stackoverflow.com/questions/9362959/sending-and-receiving-xml-data-over-tcp)あなたを助けますか? – dotNET

答えて

0

あなたはrawソケットを持つ巨大に慣れていない場合は、私のような何かをしたい:以下、抽象化のために

using (var client = new TcpClient()) 
    { 
     client.Connect("host", 2324); 
     using (var ns = client.GetStream()) 
     using (var writer = new StreamWriter(ns)) 
     { 
      writer.Write(xml); 
      writer.Write("\r\n\r\n"); 
      writer.Flush(); 
     } 
     client.Close(); 
    } 

を、あなただけの直接Socketインスタンスを使用して、手動ですべてのエンコードなどに対処したいですただSocket.Sendbyte[]を与えるだけです。

+0

おかげでミル!魅力を取りました! – meangrizzle

0

使用XML LINQの

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Xml; 
using System.Xml.Linq; 


namespace ConsoleApplication50 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 

       //<?xml version="1.0"?> 
       //<active_conditions> 
       // <condition id="12323" name="Sunny"/> 
       // <condition id="13323" name="Warm"/> 
       //</active_conditions> 

      string header = "<?xml version=\"1.0\"?><active_conditions></active_conditions>"; 

      XDocument doc = XDocument.Parse(header); 

      XElement activeCondition = (XElement)doc.FirstNode; 

      activeCondition.Add(new object[] { 
       new XElement("condition", new object[] { 
        new XAttribute("id", 12323), 
        new XAttribute("name", "Sunny") 
       }), 

       new XElement("condition", new object[] { 
        new XAttribute("id", 13323), 
        new XAttribute("name", "Warm") 
       }) 
      }); 

      string xml = doc.ToString(); 

      XDocument doc2 = XDocument.Parse(xml); 

      var results = doc2.Descendants("condition").Select(x => new 
      { 
       id = x.Attribute("id"), 
       name = x.Attribute("name") 
      }).ToList(); 

     } 
    } 

} 
関連する問題