Am still working on Forms and C# , I discover a lot of thing but not easy sometimes.
I want to populate data from XML file to a ListBox ,my problem is that i want to populate nodes attributes
exp :
For exp , in my listBox I want to see columns : Category , category_value, object , child
then for the contenant of columns only the attribute Id or titre for Category , category_value.
my code work only when i have simple XML file with data .
On the internet the code is not simple for me ( a lot of class )
Can u help me plz
VulpesPosted Oct 23, 2014, 7:15 AM
foreach(XElement obj in category_value.Elements("object"))
{
string id = obj.Attribute("id").Value;
string name = obj.Attribute("name").Value;
if (obj.Elements("child").Count() == 0)
{
ListViewItem lvi = new ListViewItem(titre, 0);
lvi.SubItems.Add(titre2);
lvi.SubItems.Add(id + " " + name);
lvi.SubItems.Add(" - ");
listView1.Items.Add(lvi);
}
else
{
foreach (XElement child in obj.Elements("child"))
{
string id2 = child.Attribute("id").Value;
ListViewItem lvi = new ListViewItem(titre, 0);
lvi.SubItems.Add(titre2);
lvi.SubItems.Add(id + " " + name);
lvi.SubItems.Add(id2);
listView1.Items.Add(lvi);
}
}
}
Nbl BzPosted Oct 23, 2014, 7:45 AM
Thank' u ! Thank' u !
Nbl BzPosted Oct 23, 2014, 3:06 AM
I Think that i hvae a question :
if objects has not child ? then a want to add just objects in the list box and nothing in the child clolumn or "-" , how can I write
if object has no children in the code ?
because , here i think that he add objects only when they have children lik the exp in the xml file :)
Rajendra TripathyPosted Oct 17, 2014, 6:48 AM
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private const string FilePath = @"C:\xmlfile.xml";
public Form1()
{
InitializeComponent();
}
///
/// Save Values in XML
///
///
///
private void btnSaveValuesInXML_Click(object sender, EventArgs e)
{
// Creates an instance of the System.Xml.XmlTextWriter class using the specified file.
var textWriter = new XmlTextWriter(FilePath, null);
// Writes the XML declaration with the version "1.0".
textWriter.WriteStartDocument();
// Write comments
textWriter.WriteComment("First Comment for XmlTextWriter Sample Example");
// Write first element
textWriter.WriteStartElement("Student");
// textWriter.WriteStartElement("Document", "Test", "RECORD");
// Write next element Name
textWriter.WriteStartElement("Name");
textWriter.WriteString(textBox1.Text);
textWriter.WriteEndElement();
// Write element Address
textWriter.WriteStartElement("Address");
textWriter.WriteString(textBox2.Text);
textWriter.WriteEndElement();
// Write element Pincode
textWriter.WriteStartElement("Pincode");
textWriter.WriteString(textBox3.Text);
textWriter.WriteEndElement();
// Ends the document.
textWriter.WriteEndDocument();
// close writer
textWriter.Close();
MessageBox.Show(@"Values got stored in XML");
}
///
/// Retrieve Items
///
///
///
private void btnRetrieveItems_Click(object sender, EventArgs e)
{
var doc = new XmlDocument();
doc.Load(FilePath);
var nodeNames = new List
var xmlNodeList = doc.SelectNodes("/Student");
// Normal Foreach Statement.
//if (xmlNodeList != null)
// foreach (System.Xml.XmlNode node in xmlNodeList)
// {
// foreach (System.Xml.XmlNode child in node.ChildNodes)
// {
// nodeNames.Add(child.InnerText);
// }
// }
// Linq Statement
if (xmlNodeList != null)
nodeNames.AddRange(from XmlNode node in xmlNodeList
from XmlNode child in node.ChildNodes
select child.InnerText);
foreach (var node in nodeNames)
{
listView1.Items.Add(node); // You can add SubItems for this also.
listBox1.Items.Add(node);
}
}
}
}
VulpesPosted Oct 17, 2014, 6:44 AM
Nbl BzPosted Oct 17, 2014, 3:38 AM
thank' a lot
Iwanna just ask u if it's possible to concatenate two attribute in a column
for exemple ; for object : we have id="0" name="zzzzzz" in the same column ?
Nbl BzPosted Oct 17, 2014, 2:20 AM
but i have this avertissement
VulpesPosted Oct 16, 2014, 12:54 PM
// .....
I couldn't determine what you're trying to do in the SelectedIndexChanged event so I've left that for now.
Nbl BzPosted Oct 16, 2014, 4:42 AM
( upload picture )
then this one ,
private void listView1_SelectedIndexChanged_2(object sender, EventArgs e)
{
XmlNode node = xmlDoc.SelectSingleNode(string.Format("//sms[@name='{0}']", listView1.SelectedItems)); }
___
the problem is it only adds items to the first column !!
Khan Abrar AhmedPosted Oct 16, 2014, 3:28 AM
http://www.java2s.com/Code/CSharp/GUI-Windows-Form/FillXMLdatatoListBox.htm