简体   繁体   中英

C# Windows Forms - read XML into checkedlistbox

I'm creating a Windows Forms application in VisualStudio an C#.

I have a XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<Team>
<TeamMember>
    <Name>Jack John</Name>
    <MailAddress>Jack@test.com</MailAddress>
    <SearchName>John, Jack</SearchName>
    <AltSearchName>Jack John 123</AltSearchName>
</TeamMember>
<TeamMember>
    <Name>Johanna Johnson</Name>
    <MailAddress>Johanna@test.com</MailAddress>
    <SearchName>Johnson, Johanne</SearchName>
    <AltSearchName>Johanna Johnson 123</AltSearchName>
</TeamMember>
<TeamMember>
    <Name>Ron Ma</Name>
    <MailAddress>Ron@test.com</MailAddress>
    <SearchName>Ma, Ron</SearchName>
    <AltSearchName></AltSearchName>
</TeamMember>
</Team>

What I want to achieve is to put all the Names (without the other informations of the XML) into one CheckedListBox. Goal is to create a program in which you can paste a long string of Persons and email adresses and the code searches this string for all TeamMembers which are in the XML. If the long string contains the TeamMembers, it will "check" the box in the CheckedListBox of the coressponding person.

What I have so far:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestMailVerteiler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataSet ds = new DataSet();
            string fileNameWithAbsolutePath = "C:/Test/Text.xml";
            ds.ReadXml(fileNameWithAbsolutePath);
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                        checkedListBox1.Items.Add(ds.Tables[0].Rows[i][j].ToString());
                    }
                }
            }

        }
    }
} 

But his (as expected) will put all information of the XML in the CheckedListBox.

What I'm thinking of:

  1. Create a internal "table/class (?)" from my xml file
  2. Use table column "Name" to fill out CheckedListBox
  3. Use other table columns to later search the input-string and match the "Names", if match was found, after pressing a button, check ListBoxes accordingly.

How can I achieve this?

The simplest thing is:

ds.ReadXml(fileNameWithAbsolutePath);
if (ds.Tables.Count > 0)
{
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        checkedListBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
    }
}

However, implementing a class is a good idea. To do this automatically, select your entire XML and copy to the clipboard. In Visual Studio, place the cursor at the point you wish to insert the classes. It should be outside the Form1 but inside the same namespace or on a separate class file. Then go to Edit (menu) and paste special > paste as XML class.

Something like that will come out:

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Team
{

    private TeamTeamMember[] teamMemberField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("TeamMember")]
    public TeamTeamMember[] TeamMember
    {
        get
        {
            return this.teamMemberField;
        }
        set
        {
            this.teamMemberField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class TeamTeamMember
{

    private string nameField;

    private string mailAddressField;

    private string searchNameField;

    private string altSearchNameField;

    /// <remarks/>
    public string Name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    public string MailAddress
    {
        get
        {
            return this.mailAddressField;
        }
        set
        {
            this.mailAddressField = value;
        }
    }

    /// <remarks/>
    public string SearchName
    {
        get
        {
            return this.searchNameField;
        }
        set
        {
            this.searchNameField = value;
        }
    }

    /// <remarks/>
    public string AltSearchName
    {
        get
        {
            return this.altSearchNameField;
        }
        set
        {
            this.altSearchNameField = value;
        }
    }
}

Now, here is a code to deserialize your xml and extract only the names in your checkedListbox:

            XmlSerializer xs = new XmlSerializer(typeof(Team));
            using (StreamReader rd = new StreamReader(MyXmlFile))
            {
                Team team = xs.Deserialize(rd) as Team;
                int n = team.TeamMember.Length;
                for (int i = 0; i < n; i++)
                {
                    checkedListBox1.Items.Add(team.TeamMember[i].Name);
                }                
            }    

You will have to insert the following references at the beggining:

using System.Xml.Serialization;
using System.IO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM