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. Reading Xml to and fro

Reading Xml to and fro

Scheduled Pinned Locked Moved C#
helpxml
15 Posts 4 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.
  • _ _Erik_

    Look at your loop:

    foreach (XmlNode PersonTag in PersonNode.ChildNodes)
    {
    sOut = PersonTag.Name + "\t" + PersonTag.FirstChild.Value;
    txtBox.Text = sOut;
    }

    You replace sOut and txtBox.Text values on each iteration so, when the loop ends you only have the last line... And you are not using any ListView control here, but a TextBox.

    S Offline
    S Offline
    squerley
    wrote on last edited by
    #5

    Hey _Erik_ thanks for you help on my last post =^) Yeah this was just to test so i could see the xml, because i was having kinda the same problem with my list view so i just wanted to test it with something simple haha im a newbee cant you tell haha I dont see it :^)

    sOut = PersonTag.Name + "\t" + PersonTag.FirstChild.Value + "\r\n";
    Or this
    sOut = PersonNode.Name + "\t" + PersonTag.FirstChild.Value + "\r\n";

    _ 1 Reply Last reply
    0
    • R RaviRanjanKr

      Yes Erik pointed you what mistake you have done whatever, take a snap of this code. it include a little bit modification and display all lines of XML file

      string sOut ="";
      XmlDocument myDoc = new XmlDocument();
      try
      {
      myDoc.Load(@"F:\Images.xml");
      }
      catch (Exception xmle)
      {
      MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
      return;
      }
      // txtBox.Text = "";
      XmlNodeList nodeList = myDoc.DocumentElement.ChildNodes;
      foreach (XmlNode PersonNode in nodeList)
      {
      foreach (XmlNode PersonTag in PersonNode.ChildNodes)
      {
      sOut += PersonTag.Name + "\t" + PersonTag.FirstChild.Value +"\r\n";

                      txtBox.Text = sOut;
                  }
               
              }
      
      S Offline
      S Offline
      ShilpaKumari
      wrote on last edited by
      #6

      Simple and smart answer

      R 1 Reply Last reply
      0
      • R RaviRanjanKr

        Yes Erik pointed you what mistake you have done whatever, take a snap of this code. it include a little bit modification and display all lines of XML file

        string sOut ="";
        XmlDocument myDoc = new XmlDocument();
        try
        {
        myDoc.Load(@"F:\Images.xml");
        }
        catch (Exception xmle)
        {
        MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
        return;
        }
        // txtBox.Text = "";
        XmlNodeList nodeList = myDoc.DocumentElement.ChildNodes;
        foreach (XmlNode PersonNode in nodeList)
        {
        foreach (XmlNode PersonTag in PersonNode.ChildNodes)
        {
        sOut += PersonTag.Name + "\t" + PersonTag.FirstChild.Value +"\r\n";

                        txtBox.Text = sOut;
                    }
                 
                }
        
        S Offline
        S Offline
        squerley
        wrote on last edited by
        #7

        Haha Now i got to print the last two lines haha

        string sOut = "";
        XmlDocument myDoc = new XmlDocument();
        try
        {
        myDoc.Load("people.xml");
        }
        catch (Exception xmle)
        {
        MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
        return;
        }
        txtBox.Text = "";
        XmlNodeList nodeList = myDoc.DocumentElement.ChildNodes;
        foreach (XmlNode PersonNode in nodeList)
        {
        foreach (XmlNode PersonTag in PersonNode.ChildNodes)
        {
        sOut += PersonTag.Name + "\t" + PersonTag.FirstChild.Value + "\r\n";
        txtBox.Text = sOut;
        }
        }

        R 1 Reply Last reply
        0
        • S squerley

          Hey _Erik_ thanks for you help on my last post =^) Yeah this was just to test so i could see the xml, because i was having kinda the same problem with my list view so i just wanted to test it with something simple haha im a newbee cant you tell haha I dont see it :^)

          sOut = PersonTag.Name + "\t" + PersonTag.FirstChild.Value + "\r\n";
          Or this
          sOut = PersonNode.Name + "\t" + PersonTag.FirstChild.Value + "\r\n";

          _ Offline
          _ Offline
          _Erik_
          wrote on last edited by
          #8

          I'm afraid none of them. Put a ListBox on your Form instead of a TextBox. This is basically a tree, so you will need a recursive method. This is a very basic sample:

          void ShowAll(XmlNodeList nodes)
          {
          foreach (XmlNode node in nodes)
          {
          listBox.Add(string.Format("{0}\t{1}", node.Name, node.Value));

              if (node.HasChildNodes)
                  ShowAll(node.ChildNodes);
          }
          

          }

          Research something about recursion in the Internet if you cannot understand it.

          1 Reply Last reply
          0
          • S squerley

            Haha Now i got to print the last two lines haha

            string sOut = "";
            XmlDocument myDoc = new XmlDocument();
            try
            {
            myDoc.Load("people.xml");
            }
            catch (Exception xmle)
            {
            MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            return;
            }
            txtBox.Text = "";
            XmlNodeList nodeList = myDoc.DocumentElement.ChildNodes;
            foreach (XmlNode PersonNode in nodeList)
            {
            foreach (XmlNode PersonTag in PersonNode.ChildNodes)
            {
            sOut += PersonTag.Name + "\t" + PersonTag.FirstChild.Value + "\r\n";
            txtBox.Text = sOut;
            }
            }

            R Offline
            R Offline
            RaviRanjanKr
            wrote on last edited by
            #9

            squerley wrote:

            Now i got to print the last two lines haha

            :confused: there something wrong. It display all lines I have checked it and then posted. :)

            S 1 Reply Last reply
            0
            • S ShilpaKumari

              Simple and smart answer

              R Offline
              R Offline
              RaviRanjanKr
              wrote on last edited by
              #10

              :-D

              1 Reply Last reply
              0
              • R RaviRanjanKr

                Yes Erik pointed you what mistake you have done whatever, take a snap of this code. it include a little bit modification and display all lines of XML file

                string sOut ="";
                XmlDocument myDoc = new XmlDocument();
                try
                {
                myDoc.Load(@"F:\Images.xml");
                }
                catch (Exception xmle)
                {
                MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                return;
                }
                // txtBox.Text = "";
                XmlNodeList nodeList = myDoc.DocumentElement.ChildNodes;
                foreach (XmlNode PersonNode in nodeList)
                {
                foreach (XmlNode PersonTag in PersonNode.ChildNodes)
                {
                sOut += PersonTag.Name + "\t" + PersonTag.FirstChild.Value +"\r\n";

                                txtBox.Text = sOut;
                            }
                         
                        }
                
                S Offline
                S Offline
                squerley
                wrote on last edited by
                #11

                This is my listView form that im trying to get populated by the xml file

                    private void frmPersonList\_Load(object sender, EventArgs e)
                    {
                        try
                        {
                            m\_xmlDoc.Load("m\_person.xml");
                        }
                        catch (Exception xmle)
                        {
                            MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                        }
                

                and i cant see the ReadInPeople method in my main form unless i use public instead of privete and if i use public i get an exception...

                        myForm.ReadInPeople();
                
                        lstPersons.View = View.Details;
                        lstPersons.Columns.Add("Fist Name", 100, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("MI", 30, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("Last Name", 80, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("Address", 160, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("City", 150, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("State", 50, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("Zip", 50, HorizontalAlignment.Center);
                        lstPersons.Columns.Add("Date and Time", 150, HorizontalAlignment.Center);
                
                        Person p;
                        for (int i = 0; i < m\_PersonReference.Length; i++)
                        {
                            p = m\_PersonReference\[i\];
                            lstPersons.Items.Add(p.FirstName);
                            lstPersons.Items\[i\].SubItems.Add(p.MI);
                            lstPersons.Items\[i\].SubItems.Add(p.LastName);
                            lstPersons.Items\[i\].SubItems.Add(p.Address);
                            lstPersons.Items\[i\].SubItems.Add(p.city);
                            lstPersons.Items\[i\].SubItems.Add(p.state);
                            lstPersons.Items\[i\].SubItems.Add(p.zip);
                            lstPersons.Items\[i\].SubItems.Add(p.time);
                        }
                    }
                
                    private void btnCancel\_Click(object sender, EventArgs e)
                    {
                       this.Close();
                    }
                
                    private void frmPersonList\_FormClosing(object sender, FormClosingEventArgs e)
                    {
                        this.m\_xmlDoc.Save("m\_person.xml");
                    }
                
                R S 2 Replies Last reply
                0
                • S squerley

                  This is my listView form that im trying to get populated by the xml file

                      private void frmPersonList\_Load(object sender, EventArgs e)
                      {
                          try
                          {
                              m\_xmlDoc.Load("m\_person.xml");
                          }
                          catch (Exception xmle)
                          {
                              MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                          }
                  

                  and i cant see the ReadInPeople method in my main form unless i use public instead of privete and if i use public i get an exception...

                          myForm.ReadInPeople();
                  
                          lstPersons.View = View.Details;
                          lstPersons.Columns.Add("Fist Name", 100, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("MI", 30, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("Last Name", 80, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("Address", 160, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("City", 150, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("State", 50, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("Zip", 50, HorizontalAlignment.Center);
                          lstPersons.Columns.Add("Date and Time", 150, HorizontalAlignment.Center);
                  
                          Person p;
                          for (int i = 0; i < m\_PersonReference.Length; i++)
                          {
                              p = m\_PersonReference\[i\];
                              lstPersons.Items.Add(p.FirstName);
                              lstPersons.Items\[i\].SubItems.Add(p.MI);
                              lstPersons.Items\[i\].SubItems.Add(p.LastName);
                              lstPersons.Items\[i\].SubItems.Add(p.Address);
                              lstPersons.Items\[i\].SubItems.Add(p.city);
                              lstPersons.Items\[i\].SubItems.Add(p.state);
                              lstPersons.Items\[i\].SubItems.Add(p.zip);
                              lstPersons.Items\[i\].SubItems.Add(p.time);
                          }
                      }
                  
                      private void btnCancel\_Click(object sender, EventArgs e)
                      {
                         this.Close();
                      }
                  
                      private void frmPersonList\_FormClosing(object sender, FormClosingEventArgs e)
                      {
                          this.m\_xmlDoc.Save("m\_person.xml");
                      }
                  
                  R Offline
                  R Offline
                  RaviRanjanKr
                  wrote on last edited by
                  #12

                  squerley wrote:

                  i cant see the ReadInPeople method in my main form unless i use public instead of privete and if i use public i get an exception...

                  will you mention exception which is occur?

                  1 Reply Last reply
                  0
                  • R RaviRanjanKr

                    squerley wrote:

                    Now i got to print the last two lines haha

                    :confused: there something wrong. It display all lines I have checked it and then posted. :)

                    S Offline
                    S Offline
                    squerley
                    wrote on last edited by
                    #13

                    haha craziness I did it on my other lab and it works fine haha Anyhow when i run my listview lab im getting this NullReferanceException in my ReadInPeople method

                        public int ReadInPeople()
                        {
                            XmlNodeList nodeList = m\_xmlDoc.DocumentElement.ChildNodes; //this line has the exception "NullReferanceException was unhandled"
                            foreach (XmlNode PersonNode in nodeList)
                            {
                                Person addPerson = new Person();
                                foreach(XmlNode PersonTag in PersonNode.ChildNodes)
                                {
                                    switch (PersonTag.Name)
                                    {
                                        case "firstName":
                                            addPerson.SetFirstName(PersonTag.FirstChild.Value);
                                            break;
                                        case "lastName":
                                            addPerson.SetLastName(PersonTag.FirstChild.Value);
                                            break;
                                        case "address":
                                            addPerson.SetAddress(PersonTag.FirstChild.Value);
                                            break;
                                        case "city":
                                            addPerson.SetCity(PersonTag.FirstChild.Value);
                                            break;
                                        case "state" :
                                            addPerson.SetState(PersonTag.FirstChild.Value);
                                            break;
                                        case "zip":
                                            addPerson.SetZip(PersonTag.FirstChild.Value);
                                            break;
                                        default:
                                            break;
                                    }
                                }
                               this.AddPerson(addPerson); //this is my add person array
                            }
                            return nodeList.Count;
                        }
                    
                    1 Reply Last reply
                    0
                    • S squerley

                      This is my listView form that im trying to get populated by the xml file

                          private void frmPersonList\_Load(object sender, EventArgs e)
                          {
                              try
                              {
                                  m\_xmlDoc.Load("m\_person.xml");
                              }
                              catch (Exception xmle)
                              {
                                  MessageBox.Show(xmle.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                              }
                      

                      and i cant see the ReadInPeople method in my main form unless i use public instead of privete and if i use public i get an exception...

                              myForm.ReadInPeople();
                      
                              lstPersons.View = View.Details;
                              lstPersons.Columns.Add("Fist Name", 100, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("MI", 30, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("Last Name", 80, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("Address", 160, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("City", 150, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("State", 50, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("Zip", 50, HorizontalAlignment.Center);
                              lstPersons.Columns.Add("Date and Time", 150, HorizontalAlignment.Center);
                      
                              Person p;
                              for (int i = 0; i < m\_PersonReference.Length; i++)
                              {
                                  p = m\_PersonReference\[i\];
                                  lstPersons.Items.Add(p.FirstName);
                                  lstPersons.Items\[i\].SubItems.Add(p.MI);
                                  lstPersons.Items\[i\].SubItems.Add(p.LastName);
                                  lstPersons.Items\[i\].SubItems.Add(p.Address);
                                  lstPersons.Items\[i\].SubItems.Add(p.city);
                                  lstPersons.Items\[i\].SubItems.Add(p.state);
                                  lstPersons.Items\[i\].SubItems.Add(p.zip);
                                  lstPersons.Items\[i\].SubItems.Add(p.time);
                              }
                          }
                      
                          private void btnCancel\_Click(object sender, EventArgs e)
                          {
                             this.Close();
                          }
                      
                          private void frmPersonList\_FormClosing(object sender, FormClosingEventArgs e)
                          {
                              this.m\_xmlDoc.Save("m\_person.xml");
                          }
                      
                      S Offline
                      S Offline
                      ShilpaKumari
                      wrote on last edited by
                      #14

                      What are you trying, please be clear. Fistly your question is you are not getting all lines or something other

                      S 1 Reply Last reply
                      0
                      • S ShilpaKumari

                        What are you trying, please be clear. Fistly your question is you are not getting all lines or something other

                        S Offline
                        S Offline
                        squerley
                        wrote on last edited by
                        #15

                        Well its kinda hard to explain, I have a windows form that people can fill in there info and click submit and the information gets stored in a local variable. Then if the user wants to see all the people they have entered into the form they click show all and this calls the form2 with the listView and shows them all the data they have entered. Now we were asked to add in a xml file, so when the main form loads it loads the xml in to the Xml object, then when the user adds more people into the form it will add them to the local variable along with adding it to the already open Xml doc. Then when the user hits show all it shows in info from the xml file along with any new people they entered in. Hope this makes a little more sense, Sorry for sounding so obtuse :^) Well i finely got it :thumbsup::thumbsup: Thanks to all that helped. You guys Rock :-D

                        modified on Monday, December 13, 2010 9:36 PM

                        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