Home > Enterprise >  Parse XML into accordion/treeview
Parse XML into accordion/treeview

Time:01-05

I'm unable to find a way to parse my XML. I'm not sure if it's the XML formatted badly or code.

I'd like to use a datagridview or treeview to display this on an about page something like this:

VersionID = v1.0.0 CCNumber = CSV-003-CC-20-09 Description = Hard Code Dilution

  1. SimoaPlateImport.cs - Hardcoded Neat for Dilutions. Updated DataTable names to match the vendor document.
  2. CalibrationPoints.cs - Changed Reagent Name textbox to combobox. Prepopulated with NF-light"
  3. CalibrationPoints.cs - Added Auto Fill Box that populates Calibrator DataGrid Automatically"

This is my XML:

<?xml version="1.0"?>
<VersionHistory>
  <version VersionID="v1.0.0">
    <CCNumber>CSV-003-CC-20-09</CCNumber>
    <Description>Initial Release</Description>
    <Changes>Inital Release</Changes>
  </version>
  <version VersionID="v1.1.0">
    <CCNumber>CSV-003-CC-20-11</CCNumber>
    <Description>Hard Code Dilution Factor</Description>
    <Changes id="0" note="SimoaPlateImport.cs - Hardcoded Neat for Dilutions.  Updated DataTable names to match the vendor document." />
    <Changes id="1" note="ClibrationPoints.cs - Changed Reagent Name textbox to combobox.  Prepopulated with NF-light" />
    <Changes id="2" note="ClibrationPoints.cs - Added Auto Fill Box that populates Calibrator DataGrid Automatically" />
  </version>
  <version VersionID="v2.0.0">
    <CCNumber>CRMA-18-03-CC-05</CCNumber>
    <Description>Updated System to run on Watson 7.6.1</Description>
    <Changes id="0" note="Added OracleData.cs class to create a Data Connector to Watson Oracle Database." />
    <Changes id="1" note="ClibrationPoints.cs - Changed Reagent Name textbox to combobox.  Prepopulated with NF-light" />
    <Changes id="2" note="ClibrationPoints.cs - Added Auto Fill Box that populates Calibrator DataGrid Automatically" />
    <Changes id="3" note="SimoaPlateImport.cs - Modified code to populate standards from Watson DB" />
    <Changes id="4" note="DataConnector.cs - Modified Form layout and supporting code" />
    <Changes id="5" note="About.cs - Created About Form with Change History" />
  </version>
</VersionHistory>

I was able to get this working: https://imgur.com/e48JldH

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace CRLDataConnector
{
    public partial class About : Form
    {
        public string filepath = "C:\\Repo\\CRLDataConnector\\CRLDataConnector 2.0\\CRLDataConnector\\VersionHistory.xml";
        public About()
        {
            XmlReader reader = XmlReader.Create(filepath);
            XmlSerializer serializer = new XmlSerializer(typeof(VersionHistory));
            VersionHistory versionHistory = (VersionHistory)serializer.Deserialize(reader);

            InitializeComponent();
            treeLoad(versionHistory);
        }

        private void About_Load(object sender, EventArgs e)
        {
            //temp();

        }
        private void treeLoad(VersionHistory versionHistory)
        {
            for (int i = 0; i <= versionHistory.versions.Count - 1; i  )

            {
                string versionID = "["   versionHistory.versions[i].id.ToString()   "] ";
                string versionDescrip = versionHistory.versions[i].Description.ToString();
                string versionCC = " [CC# "   versionHistory.versions[i].CCNumber.ToString()   "]";
                treeAbout.Nodes.Add(versionID   versionDescrip   versionCC);
                for (int x = 0; x <= versionHistory.versions[i].changes.Count - 1; x  )

                {
                    string changeID = "["   versionHistory.versions[i].changes[x].id.ToString()   "] ";
                    string changeNote = versionHistory.versions[i].changes[x].note.ToString();
                    treeAbout.Nodes[i].Nodes.Add(changeID   changeNote);
                }
            }
        }
    }
    public class VersionHistory
    {
        [XmlElement("version")]
        public List<Version> versions { get; set; }


    }
    public class Version
    {
        [XmlAttribute("VersionID")]
        public string id { get; set; }

        public string CCNumber { get; set; }

        public string Description { get; set; }

        [XmlElement("Changes")]
        public List<Changes> changes { get; set; }
    }
    public class Changes
    {
        [XmlAttribute()]
        public int id { get; set; }

        [XmlAttribute()]
        public string note { get; set; }

    }

}

CodePudding user response:

I deserialized the xml. See if you can create the treeview yourself from the classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication9
{
    class Program
    {
        static string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(VersionHistory));
            VersionHistory versionHistory = (VersionHistory)serializer.Deserialize(reader);
        }
    }
    public class VersionHistory
    {
        [XmlElement("version")]
        public List<Version> versions { get; set; }
    }
    public class Version
    {
        [XmlAttribute("VersionID")]
        public string id { get; set; }

        public string CCNumber { get; set; }

        public string Description { get; set; }

        [XmlElement("Changes")]
        public List<Changes> changes { get; set; }
    }
    public class Changes
    {
        [XmlAttribute()]
        public int id { get; set; }

        [XmlAttribute()]
        public string note { get; set; }

        [XmlText]
        public string value { get; set; }
    }
}
  •  Tags:  
  • Related