Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML

XML

Scheduled Pinned Locked Moved C#
helpxmlquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    Hi I am sending the ip address and the port no for the socket using the following syntax:

    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
    Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    newsock.Bind(ipep);

    Because of the connection problem i want to read both the ip address and the port no from an xml file.I do not exactly know how i should be storing data in an xml...In the xml file named p1.xml i have given the following:Please check whether this is right and give me your suggestions <table width="100%" border="0"> <tr> <td><address>127.0.0.1<address> <td> <tr> <tr><td> <port>10001<port> <td><tr> I am using the following code for reading the data from the xml file:

    XmlTextreader reader = new XmlTextReader("p1.xml");
    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Text: //Display the text in each element.
    // Console.WriteLine(reader.Value);
    String s=reader.Value;
    }
    }

    My doubt is that will the string variable "s" take values of the ip and port..and how do i fit it into the socket statement...please help me with this

    L M 2 Replies Last reply
    0
    • M mrithula8

      Hi I am sending the ip address and the port no for the socket using the following syntax:

      IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
      Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      newsock.Bind(ipep);

      Because of the connection problem i want to read both the ip address and the port no from an xml file.I do not exactly know how i should be storing data in an xml...In the xml file named p1.xml i have given the following:Please check whether this is right and give me your suggestions <table width="100%" border="0"> <tr> <td><address>127.0.0.1<address> <td> <tr> <tr><td> <port>10001<port> <td><tr> I am using the following code for reading the data from the xml file:

      XmlTextreader reader = new XmlTextReader("p1.xml");
      while (reader.Read())
      {
      switch (reader.NodeType)
      {
      case XmlNodeType.Text: //Display the text in each element.
      // Console.WriteLine(reader.Value);
      String s=reader.Value;
      }
      }

      My doubt is that will the string variable "s" take values of the ip and port..and how do i fit it into the socket statement...please help me with this

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That is not XML. How about: <endpoint> <ip>127.0.0.1</ip> <port>43211</port> </endpoint> And please use a better way to read the XML..


      Last modified: after originally posted -- darned angle brackets

      M 2 Replies Last reply
      0
      • M mrithula8

        Hi I am sending the ip address and the port no for the socket using the following syntax:

        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        newsock.Bind(ipep);

        Because of the connection problem i want to read both the ip address and the port no from an xml file.I do not exactly know how i should be storing data in an xml...In the xml file named p1.xml i have given the following:Please check whether this is right and give me your suggestions <table width="100%" border="0"> <tr> <td><address>127.0.0.1<address> <td> <tr> <tr><td> <port>10001<port> <td><tr> I am using the following code for reading the data from the xml file:

        XmlTextreader reader = new XmlTextReader("p1.xml");
        while (reader.Read())
        {
        switch (reader.NodeType)
        {
        case XmlNodeType.Text: //Display the text in each element.
        // Console.WriteLine(reader.Value);
        String s=reader.Value;
        }
        }

        My doubt is that will the string variable "s" take values of the ip and port..and how do i fit it into the socket statement...please help me with this

        M Offline
        M Offline
        Michael Bookatz
        wrote on last edited by
        #3

        your using HTML... look for an XML primer

        1 Reply Last reply
        0
        • L Lost User

          That is not XML. How about: <endpoint> <ip>127.0.0.1</ip> <port>43211</port> </endpoint> And please use a better way to read the XML..


          Last modified: after originally posted -- darned angle brackets

          M Offline
          M Offline
          mrithula8
          wrote on last edited by
          #4

          Hi Both the values of port and ip are integers. I am able to read all the text values from the xml I am using

          XmlReader re=new XmlReader("p1.xml");
          String s=re.Value;

          this returns all the text values stored in xml.But i want to retrieve both the port and ip and assign to 2 different integer variables.I am not sure of this..Please help me with this

          1 Reply Last reply
          0
          • L Lost User

            That is not XML. How about: <endpoint> <ip>127.0.0.1</ip> <port>43211</port> </endpoint> And please use a better way to read the XML..


            Last modified: after originally posted -- darned angle brackets

            M Offline
            M Offline
            mrithula8
            wrote on last edited by
            #5

            Hi I tried with the following code.There is no error at the compilation time but something goes wrong at the run time.I do not know how to retrieve the ip and port from the xml..Please help me with this

            public static void start_server()
            {
            XmlTextReader r1 = new XmlTextReader("p1.xml");
            XmlDocument doc1 = new XmlDocument();
            doc1.Load(r1);
            int ipadd = r1.ReadElementContentAsInt();
            int ports = r1.ReadElementContentAsInt();
            IPEndPoint ipep = new IPEndPoint(ipadd, ports);

                    Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    newsock.Bind(ipep);
                    Console.WriteLine("Waiting for a client...");
                    while (true)
                    {
                        try
                        {
                            byte\[\] data = new byte\[1024\];
                            Console.WriteLine("hai");
                            int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                            Console.WriteLine("gfgjfk");
                            Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  
            
                            data = new byte\[1024\];
                            string ss = "Welcome to the Server";
                            data = Encoding.ASCII.GetBytes(ss);
                            newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);
            
            
                            Console.WriteLine("\\nSent Acknowledgement");
                        }
                        catch (SocketException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                     }
                }
            
            L 1 Reply Last reply
            0
            • M mrithula8

              Hi I tried with the following code.There is no error at the compilation time but something goes wrong at the run time.I do not know how to retrieve the ip and port from the xml..Please help me with this

              public static void start_server()
              {
              XmlTextReader r1 = new XmlTextReader("p1.xml");
              XmlDocument doc1 = new XmlDocument();
              doc1.Load(r1);
              int ipadd = r1.ReadElementContentAsInt();
              int ports = r1.ReadElementContentAsInt();
              IPEndPoint ipep = new IPEndPoint(ipadd, ports);

                      Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                      newsock.Bind(ipep);
                      Console.WriteLine("Waiting for a client...");
                      while (true)
                      {
                          try
                          {
                              byte\[\] data = new byte\[1024\];
                              Console.WriteLine("hai");
                              int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                              Console.WriteLine("gfgjfk");
                              Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                              Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  
              
                              data = new byte\[1024\];
                              string ss = "Welcome to the Server";
                              data = Encoding.ASCII.GetBytes(ss);
                              newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);
              
              
                              Console.WriteLine("\\nSent Acknowledgement");
                          }
                          catch (SocketException e)
                          {
                              Console.WriteLine(e.Message);
                          }
                       }
                  }
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You aren't correctly specifying which element to read. Try the XPath trick I mentioned earlier, it should always work (provided, of course, that your XPath matches your actual XML)

              M 1 Reply Last reply
              0
              • L Lost User

                You aren't correctly specifying which element to read. Try the XPath trick I mentioned earlier, it should always work (provided, of course, that your XPath matches your actual XML)

                M Offline
                M Offline
                mrithula8
                wrote on last edited by
                #7

                Hi I retrieved port and ip using

                            for (int i = 0; i < xmlnode.Count; i++)
                            {
                                st = xmlnode\[i\].FirstChild.InnerText;
                                st1 = xmlnode\[i\].LastChild.InnerText;
                            }
                       
                        ipadd = Convert.ToInt32(st);
                        ports = Convert.ToInt32(st1);
                        IPEndPoint ipep = new IPEndPoint(ipadd, ports);//ipadd=127.0.0.1 port=10001
                

                It gives me an exception"Input String was not in a correct format" but the server(.exe) opens up.In the server code i have used

                IPEndPoint ipep = new IPEndPoint(ipadd, ports);

                to open the connection with the client.For this opened server how should i change the following statement on the client

                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);

                How should i specify the port and ip on the client?i.e for the server opened on port 10001 and ip=127.0.0.1 (obtained from xml) how should i modify the client?Please give me your suggestions

                L 1 Reply Last reply
                0
                • M mrithula8

                  Hi I retrieved port and ip using

                              for (int i = 0; i < xmlnode.Count; i++)
                              {
                                  st = xmlnode\[i\].FirstChild.InnerText;
                                  st1 = xmlnode\[i\].LastChild.InnerText;
                              }
                         
                          ipadd = Convert.ToInt32(st);
                          ports = Convert.ToInt32(st1);
                          IPEndPoint ipep = new IPEndPoint(ipadd, ports);//ipadd=127.0.0.1 port=10001
                  

                  It gives me an exception"Input String was not in a correct format" but the server(.exe) opens up.In the server code i have used

                  IPEndPoint ipep = new IPEndPoint(ipadd, ports);

                  to open the connection with the client.For this opened server how should i change the following statement on the client

                  IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);

                  How should i specify the port and ip on the client?i.e for the server opened on port 10001 and ip=127.0.0.1 (obtained from xml) how should i modify the client?Please give me your suggestions

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Ok debug your code, put a breakpoint on "ipadd = Convert.ToInt32(st);" and watch the value of st and stl. You may be in for a surprise. To solve this problem.. just start using SelectSingle on an XmlDocument. It's at least 10 times as easy to get right. As to specifying the ip/port in the client - the exact same way you did it in the server will do fine.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups