0

I hope I'm not repeating this question but I couldn't find something that would help me.

I have the following .xml that I'd like to deserialize into my class.

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <buildings>
        <building>
            <name>Name</name>
            <id>1</id>
            <build_time>750</build_time>
            <time_factor>1.2</time_factor>
        </building>
        <building>
            <name>Name</name>
            <id>2</id>
            <build_time>150</build_time>
            <time_factor>1.8</time_factor>
        </building>
        <building>
            <name>Name</name>
            <id>3</id>
            <build_time>950</build_time>
            <time_factor>1.4</time_factor>
        </building>
    </buildings>
</config>

I would like to load name, id, building_time and time_factor from the element that has id = 2 into the following class.

public class Test
{
    public string name { get; set; }
    public int id { get; set; }
    public int build_time { get; set; }
    public double time_factor { get; set; }
}

What would be the best approach to do this task? Thank you.

gpenner
  • 11
  • 1
  • You need to provide a minimum work3ed example. What have you tried so far? Have you done any research on using `XPath` to isolate nodes based on parameters? – Andrew Truckle Apr 16 '17 at 17:18
  • 1
    Possible duplicate of [How to Deserialize XML document](http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) –  Apr 16 '17 at 17:21
  • @AndrewTruckle sorry, I forgot to include it on my main post, I was trying to deserialize it but I was getting an error saying something about my constructor. But jdweng posted something that helped me. – gpenner Apr 16 '17 at 19:11

2 Answers2

0

You can load the XML into a System.Xml.XmlDocument (https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx) class instance and read the specific needed nodes into your C# object(s).

Another alternative, is that you may use XPATH (https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx).

Hope it helps.

asewefy
  • 109
  • 9
0

Try following :

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test2.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Test test1 = doc.Descendants("building")
                .Where(x => (int)x.Element("id") == 1)
                .Select(x => new Test() {
                    name = (string)x.Element("name"),
                    id = (int)x.Element("id"),
                    build_time = (int)x.Element("build_time"),
                    time_factor = (double)x.Element("time_factor")
                }).FirstOrDefault();
        }
    }
    public class Test
    {
        public string name { get; set; }
        public int id { get; set; }
        public int build_time { get; set; }
        public double time_factor { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20