OK, I have my XML Deserializer working, I can see the data in the object. I'm trying to set it as the datasource of a combo box now, but it's not working.
I used the XSD to create the class.
I even made a List<> but I can't find the right DisplayMemeber etc to use. The item fields are buried pretty deep.
I'm new to using objects, some of the elements support ToList but that seems odd too.
This may be overkill for my little project, I just want to practice using XML in different ways, I'm wanting to populate a listBox and when I click the item it displays the body of the text element in a richTextBox. The data will be stored in an XML file.
Loading
JasonPosted Sep 13, 2011, 10:51 PM
private void loadlist()
{
this.cmbProject.Items.Clear();
this.listBox1.Items.Clear();
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
xmlDoc.Load("projects.xml"); //* load the XML document from the specified file.
//* Get elements.
XmlNodeList nl = xmlDoc.GetElementsByTagName("storedproc");
//Add all the projects to the combo box from the proj attribute
for (int i = 0; i < nl.Count; i++)
{
//Check to see if it already exists in the combo box, if not add it
if (!this.cmbProject.Items.Contains(nl[i].Attributes["proj"].Value))
cmbProject.Items.Add(nl[i].Attributes["proj"].Value);
}
}
private void cmbProject_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
xmlDoc.Load("projects.xml"); //* load the XML document from the specified file.
//* Get elements.
XmlNodeList nl = xmlDoc.GetElementsByTagName("storedproc");
this.listBox1.Items.Clear();
for (int i = 0; i < nl.Count; i++)
{
if (cmbProject.SelectedItem.ToString() == nl[i].Attributes["proj"].Value) //if the combo box selection = the current node attribute "proj" value
{
listBox1.Items.Add(nl[i].Attributes["name"].Value); //Gets the stored proc name
txtProject.Text = nl[i].Attributes["proj"].Value; //Gets the project name from the attribute
}
}
}
Also a button to save changes and keep my SQL format safe using CDATA
private void btnSave_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
xmlDoc.Load("projects.xml"); //* load the XML document from the specified file.
XmlElement xRoot = xmlDoc.DocumentElement; //Creates the base xmlDoc object
XmlElement xStoredProc = xmlDoc.CreateElement("storedproc"); //Creates the element based on an existing element in your XML file
XmlCDataSection xValue = xmlDoc.CreateCDataSection(this.richTextBox1.Text); //Creates the CDATA section
xStoredProc.SetAttribute("name", textBox1.Text); //Saves the attribute value for the "name" attribute
xStoredProc.SetAttribute("proj", txtProject.Text); //Same as above but usinge "proj"
xRoot.AppendChild(xStoredProc); //adds the attributes to the node
xRoot.LastChild.AppendChild(xValue); //adds the CDATA section to the node
xmlDoc.Save("projects.xml");
this.button1.Visible = false;
this.listBox1.Items.Clear();
loadlist();
}
JasonPosted Sep 13, 2011, 7:52 PM
Sam HobbsPosted Sep 13, 2011, 5:04 PM
I have not used XSD to generate classes and then use that code. I have written code to serialize and deserialize simple XML; I wrote an article about that. I recently learned about XSD and I have used it to generate a schema from XML and classes from the schema but that is as far as I got. I suppose that I can try to create a data source from the classes but I probably need to spend a little more time analyzing the data. Try using the Data Sources window; hopefully it will work.
Also, I assume you are using the XmlSerializer class, since you are using XSD and you say you have a "XML Deserializer working". If you are not using the XmlSerializer class, then what are you using?
JasonPosted Sep 13, 2011, 4:30 PM
Sam HobbsPosted Sep 13, 2011, 2:30 PM
Mayur DighePosted Sep 13, 2011, 12:05 PM
Try this Code .....
DataSet Dset = new DataSet();
Dset.ReadXml("..\\..\\XMLData.xml");
for (int item=0; item < Dset.Tables.Count;item++ )
{
comboBox1.Items.Add(Dset.Tables[item].TableName.ToString());
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.DataSource = null;
listBox1.DataSource = Dset.Tables[comboBox1.Text];
}
privatevoid listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//this will work if you have "Body" Column in you XML File,
//you can change the name of the field
richTextBox1.Text=Dset.Tables[comboBox1.Text]. Rows[listBox1.SelectedItem().toString()]["Body"].ToString(); }