简体   繁体   English

C#-在字典列表框中显示XML文件中的数据

[英]C# - Displaying data from an XML file in a listbox from a Dictionary

I have an XML file with data in it and I want to display ONLY the 's of the organisation on one page and display the corresponding data (Address, ID) on another page. 我有一个包含数据的XML文件,我只想在一页上显示组织的,而在另一页上显示相应的数据(地址,ID)。 In Textboxes. 在文本框中。

XML:
<Data>
  <Organisation>
<Name>Accident Compensation Corporation</Name>
    <ID> 022 12345678 </ID>
    <Address> 220 Bunny Street</Address>
    </Organisation>

  <Organisation>
    <Name>Test 2</Name>
    <Address> 50 Lambton Quay</Address>
    <ID> 021 8972468739 </ID>
  </Organisation>


</Data>

I have ripped this data into my C# and it is currently stored in a dictionary. 我已将此数据提取到C#中,并且当前存储在字典中。 In a separate class C# file. 在单独的C#类文件中。

public class Organisation
{
    public int id;
    public string name;
    public string address;

    public Organisation(int id, string name, string address)
    {
        this.id = id;
        this.name = name;
        this.address = address;
    }
}

} }

This is what I have for loading the XML nodes and trying to display them in the listbox. 这就是我加载XML节点并尝试在列表框中显示它们的条件。 However it cannot do this in the "try" and skips to the "Catch" displaying "Error" 但是,它无法在“尝试”中执行此操作,并跳至显示“错误”的“捕获”

                XmlNodeList names = doc.GetElementsByTagName("Name");
                XmlNodeList ids = doc.GetElementsByTagName("ID");
                XmlNodeList addresses = doc.GetElementsByTagName("Address");





                for (int i = 0; i < names.Count; i++)
                {

                    Organisation org = new Organisation(int.Parse(ids[i].InnerText), names[i].InnerText, addresses[i].InnerText);
                    Database.data.Add(org);

                }

                foreach (Organisation org in Database.data)
                {
                    directoryBox.Items.Add(org.name);

                }
            }
            catch
            {
                Response.Write("ERROR");
            }
        }
    }

This is the code on the second page, placing them in textboxes 这是第二页上的代码,将它们放在文本框中

 if (!IsPostBack)
        {
            foreach (Organisation org in Database.data)
            {
                if (org.name.Equals(Session["Org"]))
                {
                    orgLabel.Text = org.name;
                      lastcontractorBox.Text = org.id.ToString();
                    buildingAddress.Text = org.address;
                }
            }
        }

The XML does work as I am able to display names using textbox1.Items.Add(names[i].InnerText); XML确实可以使用textbox1.Items.Add(names [i] .InnerText);来显示名称。

Any help would be appreciated! 任何帮助,将不胜感激!

I made two changes to your code posted. 我对您发布的代码进行了两项更改。 1) I added to Organization a constructor with no parameters to make the xml linq simpler. 1)我向Organization添加了一个没有参数的构造函数,以简化xml linq。 2) I changed ID from a int to a string since the id's have spaces in the number. 2)我将ID从int更改为字符串,因为id在数字中有空格。

I suspect your xml has a namespace which may be the reason your code isn't working so I made my code very robust to handle cases where the are no namespaces and with xml that has namespaces. 我怀疑您的xml具有名称空间,这可能是您的代码无法正常工作的原因,因此我使我的代码非常健壮,可以处理没有名称空间且具有名称空间的xml的情况。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string xml = "<Data>" + "<Organisation>" + "<Name>Accident Compensation Corporation</Name>" + "<ID> 022 12345678 </ID>" + "<Address> 220 Bunny Street</Address>" + "</Organisation>" + "<Organisation>" + "<Name>Test 2</Name>" + "<Address> 50 Lambton Quay</Address>" + "<ID> 021 8972468739 </ID>" + "</Organisation>" + "</Data>"; XElement data = XElement.Parse(xml); List<Organisation> organizations = new List<Organisation>(); organizations = data.Descendants().Where(x => x.Name.LocalName == "Organisation").Select(y => new Organisation() { name = (string)y.Element("Name"), address = (string)y.Element("Address"), id = (string)y.Element("ID") }).ToList(); } } public class Organisation { public string id; public string name; public string address; public Organisation() { } public Organisation(string id, string name, string address) { this.id = id; this.name = name; this.address = address; } } } 

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

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