How to use xml id to fill class object?

不打扰是莪最后的温柔 提交于 2019-12-11 17:55:29

问题


I have the following class definition in C#:

class Supervisor
{
    public string Id { set; get; }
    public string Name { set; get; }
    public int Contracts { set; get; }
    public long Volume { set; get; }
    public int Average { get; }
}

I also have this xml document :

 <digital-sales xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <supervisor id="1236674">
        <Name>Hiroki</Name>
        <Contract>11</Contract>
        <Volume>1036253</Volume>
        <Average>94205</Average>
    </supervisor>
    <supervisor id="123459">
        <Name>Ayumi</Name>
        <Contract>5</Contract>
        <Volume>626038</Volume>
        <Average>125208</Average>
    </supervisor> ...
 </digital-sales>

Inside the main I create object for each supervisor and fill its data by searching id. However I always get null as result. This is my Main method:

    static void Main(string[] args)
    {

        // load the document
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"C:\Users\Hideki\ShinDeta.xml");


        Supervisor ayumi = new Supervisor();

  // this line return null 
        XmlNode ayumiNode = xmlDoc.GetElementById("1236674");

      //     ayumi.Id = (supply expression here);
     //      ayumi.Name = (supply expression here);
    //       ayumi.Contracts = (supply expression here);
   //        ayumi.Volume = (supply expression here);
  //         ayumi.Average = (supply expression here);

    }

Can someone points me to the shortest way to achieve this?. No linq syntax please I am not familiar with it at all.


回答1:


You could use Xml serialization to accomplish this. Here's an example

    [XmlType("supervisor")]
    public class Supervisor
    {
        [XmlAttribute("id")]
        public string Id { set; get; }

        public string Name { set; get; }

        [XmlElement("Contract")]
        public int Contracts { set; get; }

        public long Volume { set; get; }

        public int Average { set; get; }
    }

        static void Main(string[] args)
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(List<Supervisor>), new XmlRootAttribute("digital-sales"));
                using (FileStream fileStream = new FileStream(@"C:\temp\ShinDeta.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var supervisors = xs.Deserialize(fileStream) as List<Supervisor>;
                    foreach (Supervisor supervisor in supervisors)
                    {
                        Debug.WriteLine($"Id: {supervisor.Id}, Name: {supervisor.Name}");
                    }
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }           
        }

This outputs:

Id: 1236674, Name: Hiroki
Id: 123459, Name: Ayumi


来源:https://stackoverflow.com/questions/54090867/how-to-use-xml-id-to-fill-class-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!