How do i add data from XML to list<>?

前端 未结 2 1047
陌清茗
陌清茗 2021-01-28 09:31

I try to read from an xml file, but its very clonky and a lot of the data I get is in bunch from a child. I get the Name, Age, and so on in one and therefor I can\'t add it to a

相关标签:
2条回答
  • 2021-01-28 09:50

    Here is a simple example of an XML import. After this code executes, results will reflect if people were found (true or false), and msg will be a list of error messages (or empty if success).

                var results = true;
            var msg = new List<string>(0);
            XDocument aDocument = null;
            try
            {
                aDocument = XDocument.Load("");
            }
            catch (Exception e)
            {
                results = false;
                msg.Add(string.Format("Unable to open file:{0}", ""));
                msg.Add(e.Message);
            }
    
            if (aDocument != null)
            {
                var thePeople = aDocument.Descendants("Person").ToArray();
    
                if (thePeople.Any())
                {
                    // there were people in the file. People is an array of XML Nodes containing your person.
                    foreach (var pers in thePeople.Select(p => new Person().FromXML(p)))
                    {
                        // here is a person
                    }
                }
                else
                {
                    results = false;
                    msg.Add("No people found.");
                }
    
            }
    

    Hope this helps.

    Addition.

    You could do something like this in your Person Class. I've added code to the original to illustrate usage.

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
    
        public XElement ToXML()
    {
    
        return new XElement("Person", "Name", Name,
                   new XElement("Age", Age),
                   new XElement("Sex", Sex));
    }
    
        public Person FromXML(XElement node)
        {
            try { Name = node.Element("Name").Value; }
            catch { Name = "Not Found"; }
            try { Age = Convert.ToInt16(node.Element("Age").Value); }
            catch { Age = -1; }
            try { Sex = node.Element("Sex").Value; }
            catch { Sex = ""; }
            return this;
        }
    }
    
    0 讨论(0)
  • Let's assume you have this class:

    public class Person
    {
        public string Name { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
    }
    

    Then, to load your XML file, you could do something like:

    var doc = XDocument.Load("path to your file");
    
    var people = doc.Root
        .Descendants("person")
        .Select(node => new Person
        {
            Name = node.Element("name").Value,
            Sex = node.Element("sex").Value,
            Age = int.Parse(node.Element("age").Value)
        })
        .ToList();
    

    See https://msdn.microsoft.com/en-us/library/bb353813.aspx

    0 讨论(0)
提交回复
热议问题