Question

I want to cast a string from xml file to be casted to ContactPersonType

See for loop for where string needs to be casted

public class ContactPersonType
{
    private String _id;
    public String ID
    {
        get { return _id; }
        set { _id = value; }
    }

    private String _name;
    public String Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

//ContactPerson class

private ContactPersonType _jobRole;
        public ContactPersonType JobRole
        {
            get { return _jobRole; }
            set { _jobRole = value; }
        }


public static ObservableCollection<ContactPerson> getContactPerson()
        {
            ObservableCollection<ContactPerson> ContactPersons = new ObservableCollection<ContactPerson>();
            XmlDocument doc = new XmlDocument();
            doc.Load("contactpersoon.xml");

            XmlNodeList elemList = doc.GetElementsByTagName("contact");
            for (int i = 0; i < elemList.Count; i++)
            {
                 //this needs to be casted to ContactPersonType
                contactPerson.JobRole = elemList[i]["jobrole"].InnerText;
            }
            return ContactPersons;
        }
Was it helpful?

Solution

I'm not really familliar with the way you read the XML elements so this code might need some tweaking but this should be along the lines of what you're looking for (also took the liberty of making your properties autoimplemented for higher code readability)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace Program
{
    public class ContactPersonType
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }

    public class ContactPerson
    {
        public ContactPersonType JobRole { get; set; }

        public static ObservableCollection<ContactPerson> GetContactPerson()
        {
            var contactPersons = new ObservableCollection<ContactPerson>();
            XElement doc = XElement.Load("contactpersoon.xml");
            var contacts = doc.Elements("contact");

            for (int i = 0; i < contacts.Count(); i++)
            {
                contactPersons.Add(new ContactPerson
                {
                    JobRole = new ContactPersonType
                    {
                        ID = i.ToString(),
                        Name = contacts.ElementAt(i).Element("jobrole").Value
                    }
                });
            }

            return contactPersons;
        }
    }
}

Depending on how you set the ID property you could possibly rewrite the loop as a foreach. For more info on XElement and it's members, check out http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx and http://msdn.microsoft.com/en-us/vstudio/bb688087.aspx

OTHER TIPS

Can you not deserialize the XML to the .NET class with a helper such as the one shown below?

public T Deserialize<T>(string fileName)
        {
            try
            {           
                XmlSerializer sx = new XmlSerializer(typeof(T)); //or T.GetType?
                StreamReader sr = new StreamReader(fileName);
                var data = sx.Deserialize(sr);
                sr.Close();

                return (T)data;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

I hate XmlDocument, so always deserialize where possible (though a potential alternative to i do like is XDocument).

EDIT - the usage would be something like:

var item = Deserialize<ContactPersonType>(fileName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top