Can you describe your problem again? I could not get your question from your existing description.
Just a guess, if you want to get xml into c#, you can follow the steps below:
Modify the xml format. The xml you showed is not a valid xml. I made some modifications to it and got this:
<?xml version="1.0" encoding="UTF-8"?>
<root >
<message>
<header>
<to>companyReceiver</to>
<from>companySender</from>
<type>saveInvoice</type>
</header>
<body>
<saveInvoice>
<invoice eventid="10000002" date="12-18-2000" number="123">
<address country="US"/>
<name>John Smith</name>
<street>123 George St.</street>
<items>
<item number="1" active="true">
<name>IBM A20 Laptop</name>
<quantity>88</quantity>
<USPrice>2000.00</USPrice>
</item>
<item number="2" active="false">
<name>Acer Laptop</name>
<quantity>14</quantity>
<USPrice>500.00</USPrice>
</item>
</items>
</invoice>
</saveInvoice>
</body>
</message>
<message>
<header>
<to>companyReceiver</to>
<from>companySender</from>
<type>saveInvoice</type>
</header>
<body>
<saveInvoice>
<invoice eventid="10000003" date="12-18-2000" number="123">
<address country="US"/>
<name>Brown</name>
<street>123 Ewing street</street>
<items>
<item number="1" active="true">
<name>IBM A20 Laptop</name>
<quantity>88</quantity>
<USPrice>2000.00</USPrice>
</item>
<item number="2" active="false">
<name>Acer Laptop</name>
<quantity>14</quantity>
<USPrice>500.00</USPrice>
</item>
</items>
</invoice>
</saveInvoice>
</body>
</message>
</root>
Copy all the xml, open Visual Studio, and find Edit -> Paste Special, in the menu. After you click, the corresponding class will be generated according to the xml structure.
This feature is available with the installation of the ASP.NET and web development workload. If it is not in your menu, you can install this workload. If you don’t want to install it, please tell me and I will provide the generated class directly to you, it is a bit long.
Then you can use simple code to get it into c#:
XmlSerializer serializer = new XmlSerializer(typeof(root));
//read
using (FileStream stream = File.OpenRead(@"D:\test\xml\12.xml"))
{
root dezerializedList = (root)serializer.Deserialize(stream);
Console.WriteLine();
}
//write
using (FileStream stream = File.OpenWrite("filename"))
{
root list = new root();
serializer.Serialize(stream, list);
}
Update:
Maybe something like this:
TcpListener server = null;
try
{
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
Console.Write("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
XmlSerializer serializer = new XmlSerializer(typeof(root));
MemoryStream memStream = new MemoryStream(bytes);
root resultingMessage = (root)serializer.Deserialize(memStream);
//Put the acquired data into the queue
}
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
As for the selection of queues, I think BlockingCollection mentioned in another thread is a good choice. It uses ConcurrentQueue by default. You can also specify other queues in the constructor or specify the upper limit of the number of queue elements.
50287-root.txt
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.