简体   繁体   English

C# Windows Forms - 将 XML 读入选中列表框

[英]C# Windows Forms - read XML into checkedlistbox

I'm creating a Windows Forms application in VisualStudio an C#.我正在 VisualStudio 中创建一个 Windows Forms 应用程序 C#。

I have a XML file like this:我有一个这样的 XML 文件:

<?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.我想要实现的是将所有名称(没有 XML 的其他信息)放入一个 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.目标是创建一个程序,您可以在其中粘贴一长串 Persons 和 email 地址,代码会在该字符串中搜索 XML 中的所有 TeamMembers。如果长字符串包含 TeamMembers,它将“选中”中的框对应人的 CheckedListBox。

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.但是他(正如预期的那样)会将 XML 的所有信息放在 CheckedListBox 中。

What I'm thinking of:我在想什么:

  1. Create a internal "table/class (?)" from my xml file从我的 xml 文件创建一个内部“表/类(?)”
  2. Use table column "Name" to fill out CheckedListBox使用表列“名称”填写 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.但是,实现 class 是个好主意。 To do this automatically, select your entire XML and copy to the clipboard.要自动执行此操作,select 您的整个 XML 并复制到剪贴板。 In Visual Studio, place the cursor at the point you wish to insert the classes.在 Visual Studio 中,将 cursor 放在要插入类的位置。 It should be outside the Form1 but inside the same namespace or on a separate class file.它应该在 Form1 之外,但在同一命名空间内或在单独的 class 文件中。 Then go to Edit (menu) and paste special > paste as XML class.然后 go 编辑(菜单)并粘贴特殊 > 粘贴为 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:现在,这是反序列化您的 xml 并仅提取您的 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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