简体   繁体   中英

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. 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. In a separate class C# file.

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. 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);

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. 2) I changed ID from a int to a string since the id's have spaces in the number.

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.

 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; } } } 

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