C# endless list is filled and taken - concept with xml object

Markus Freitag 3,786 Reputation points
2020-12-18T08:44:12.737+00:00

Hello,
I receive a string that I have to buffer. Then process it in sequence. What is the best way to do this in C# WPF desktop?

In the example, a number is incremented in the task. But I need to parse and process a passed string(XML). How is something like this implemented?
How to transfer?

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

49407-concept.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,917 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-12-21T02:29:20.893+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.