Reading Xml to and fro
-
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.
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"; -
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; } }
Simple and smart answer
-
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; } }
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;
}
} -
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";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.
-
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;
}
}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. :)
-
Simple and smart answer
:-D
-
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; } }
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"); }
-
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"); }
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?
-
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. :)
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; }
-
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"); }
What are you trying, please be clear. Fistly your question is you are not getting all lines or something other
-
What are you trying, please be clear. Fistly your question is you are not getting all lines or something other
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