xml转换为c#对象

本文关键字:对象 转换 xml | 更新日期: 2023-09-27 17:50:33

如何将此xml记录对象转换为List<dp_donorsearch>。我目前的代码是下面,但我想找到最好的方法来做到这一点.....任何想法?

<result>
    <record>
        <field name="donor_id" id="donor_id" value="9879" />
        <field name="first_name" id="first_name" value="Trix5647" />
        <field name="last_name" id="last_name" value="Rabbit657" />
        <field name="email" id="email" value="test@asd..com" />
        <field name="business_phone" id="business_phone" value="" />
        <field name="mobile_phone" id="mobile_phone" value="" />
        <field name="home_phone" id="home_phone" value="" />
        <field name="address" id="address" value="Street S.W. " />
        <field name="address2" id="address2" value="" />
        <field name="city" id="city" value="Quaker" />
        <field name="state" id="state" value="PA" />
        <field name="zip" id="zip" value="1234" />
        <field name="country" id="country" value="USA" />
    </record>
</result>

c#代码

 public class dp_donorsearch
    {
        public string donor_id { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string email { get; set; }
        public string business_phone { get; set; }
        public string mobile_phone { get; set; }
        public string home_phone { get; set; }
        public string address { get; set; }
        public string address2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public string country { get; set; } 
        public static List<dp_donorsearch> Load(string url)
        {
            var list = new List<dp_donorsearch>();
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(url);
            list.AddRange(from XmlNode record in xmlDoc.SelectNodes("result/record")
                          select new dp_donorsearch
                          {
                              donor_id = ReadField(record, "donor_id"),
                              first_name = ReadField(record, "first_name"),
                              last_name = ReadField(record, "last_name"),
                              email = ReadField(record, "email"),
                              business_phone = ReadField(record, "business_phone"),
                              mobile_phone = ReadField(record, "mobile_phone"),
                              home_phone = ReadField(record, "home_phone"),
                              address = ReadField(record, "address"),
                              address2 = ReadField(record, "address2"),
                              city = ReadField(record, "city"),
                              state = ReadField(record, "state"),
                              zip = ReadField(record, "zip"),
                              country = ReadField(record, "country")
                          });
            return list;
        }

        private static string ReadField(XmlNode node, string nodeName)
        {
            var selectSingleNode = node.SelectSingleNode("field[@name = '" + nodeName + "']");
            if (selectSingleNode != null && selectSingleNode.Attributes != null)
                return selectSingleNode.Attributes["value"].Value;
            return string.Empty;
        }
    }

xml转换为c#对象

如何将其反序列化到您的类结构中

参考xml命名空间

 using System.Xml;
 using System.Xml.Serialization;

进行反序列化
        string input =
            "<result>'n" +
               "<record>" +
                   "<field name='"donor_id'" id='"donor_id'" value='"9879'" />" +
                   "<field name='"first_name'" id='"first_name'" value='"Trix5647'" />" +
                   "<field name='"last_name'" id='"last_name'" value='"Rabbit657'" />" +
                   "<field name='"email'" id='"email'" value='"test@asd..com'" />" +
                   "<field name='"business_phone'" id='"business_phone'" value='"'" />" +
                   "<field name='"mobile_phone'" id='"mobile_phone'" value='"'" />" +
                   "<field name='"home_phone'" id='"home_phone'" value='"'" />" +
                   "<field name='"address'" id='"address'" value='"Street S.W. '" />" +
                   "<field name='"address2'" id='"address2'" value='"'" />" +
                   "<field name='"city'" id='"city'" value='"Quaker'" />" +
                   "<field name='"state'" id='"state'" value='"PA'" />" +
                   "<field name='"zip'" id='"zip'" value='"1234'" />" +
                   "<field name='"country'" id='"country'" value='"USA'" />" +
               "</record>" +
           "</result>";
        XmlSerializer serializer = new XmlSerializer(typeof(Result));
        Result result = serializer.Deserialize(new System.IO.StringReader(input)) as Result;
类定义

[XmlRoot("result")]
public class Result
{
    [XmlElement("record")]
    public Record[] Records { get; set; }
}
public class Record
{
    [XmlElement("field")]
    public Field[] Fields { get; set; }
}
public class Field
{
    [XmlAttribute("name")]
    public string Name { get; set; }
    [XmlAttribute("id")]
    public string Id { get; set; }
    [XmlAttribute("value")]
    public string Value { get; set; }
}

这是一个有趣的解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<result>" +
                   "<record>" +
                       "<field name='"donor_id'" id='"donor_id'" value='"9879'" />" +
                       "<field name='"first_name'" id='"first_name'" value='"Trix5647'" />" +
                       "<field name='"last_name'" id='"last_name'" value='"Rabbit657'" />" +
                       "<field name='"email'" id='"email'" value='"test@asd..com'" />" +
                       "<field name='"business_phone'" id='"business_phone'" value='"'" />" +
                       "<field name='"mobile_phone'" id='"mobile_phone'" value='"'" />" +
                       "<field name='"home_phone'" id='"home_phone'" value='"'" />" +
                       "<field name='"address'" id='"address'" value='"Street S.W. '" />" +
                       "<field name='"address2'" id='"address2'" value='"'" />" +
                       "<field name='"city'" id='"city'" value='"Quaker'" />" +
                       "<field name='"state'" id='"state'" value='"PA'" />" +
                       "<field name='"zip'" id='"zip'" value='"1234'" />" +
                       "<field name='"country'" id='"country'" value='"USA'" />" +
                   "</record>" +
                  "<record>" +
                       "<field name='"donor_id'" id='"donor_id'" value='"9879'" />" +
                       "<field name='"first_name'" id='"first_name'" value='"Trix5647'" />" +
                       "<field name='"last_name'" id='"last_name'" value='"Rabbit657'" />" +
                       "<field name='"email'" id='"email'" value='"test@asd..com'" />" +
                       "<field name='"business_phone'" id='"business_phone'" value='"'" />" +
                       "<field name='"mobile_phone'" id='"mobile_phone'" value='"'" />" +
                       "<field name='"home_phone'" id='"home_phone'" value='"'" />" +
                       "<field name='"address'" id='"address'" value='"Street S.W. '" />" +
                       "<field name='"address2'" id='"address2'" value='"'" />" +
                       "<field name='"city'" id='"city'" value='"Quaker'" />" +
                       "<field name='"state'" id='"state'" value='"PA'" />" +
                       "<field name='"zip'" id='"zip'" value='"1234'" />" +
                       "<field name='"country'" id='"country'" value='"USA'" />" +
                   "</record>" +
               "</result>";
            XDocument doc = XDocument.Parse(input);

            var listDict = doc.Descendants("record").AsEnumerable()
                .Select(x => x.Descendants("field")
                .GroupBy(y => y.Attribute("name").Value, z => z.Attribute("value").Value)
                .ToDictionary(y => y.Key, z => z.FirstOrDefault())
                .ToList());

        }
    }
}
​

更像你原来的解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<result>" +
                   "<record>" +
                       "<field name='"donor_id'" id='"donor_id'" value='"9879'" />" +
                       "<field name='"first_name'" id='"first_name'" value='"Trix5647'" />" +
                       "<field name='"last_name'" id='"last_name'" value='"Rabbit657'" />" +
                       "<field name='"email'" id='"email'" value='"test@asd..com'" />" +
                       "<field name='"business_phone'" id='"business_phone'" value='"'" />" +
                       "<field name='"mobile_phone'" id='"mobile_phone'" value='"'" />" +
                       "<field name='"home_phone'" id='"home_phone'" value='"'" />" +
                       "<field name='"address'" id='"address'" value='"Street S.W. '" />" +
                       "<field name='"address2'" id='"address2'" value='"'" />" +
                       "<field name='"city'" id='"city'" value='"Quaker'" />" +
                       "<field name='"state'" id='"state'" value='"PA'" />" +
                       "<field name='"zip'" id='"zip'" value='"1234'" />" +
                       "<field name='"country'" id='"country'" value='"USA'" />" +
                   "</record>" +
                  "<record>" +
                       "<field name='"donor_id'" id='"donor_id'" value='"9879'" />" +
                       "<field name='"first_name'" id='"first_name'" value='"Trix5647'" />" +
                       "<field name='"last_name'" id='"last_name'" value='"Rabbit657'" />" +
                       "<field name='"email'" id='"email'" value='"test@asd..com'" />" +
                       "<field name='"business_phone'" id='"business_phone'" value='"'" />" +
                       "<field name='"mobile_phone'" id='"mobile_phone'" value='"'" />" +
                       "<field name='"home_phone'" id='"home_phone'" value='"'" />" +
                       "<field name='"address'" id='"address'" value='"Street S.W. '" />" +
                       "<field name='"address2'" id='"address2'" value='"'" />" +
                       "<field name='"city'" id='"city'" value='"Quaker'" />" +
                       "<field name='"state'" id='"state'" value='"PA'" />" +
                       "<field name='"zip'" id='"zip'" value='"1234'" />" +
                       "<field name='"country'" id='"country'" value='"USA'" />" +
                   "</record>" +
               "</result>";
            List<dp_donorsearch> list = dp_donorsearch.Load(input);

        }
        public class dp_donorsearch
        {
            public string donor_id { get; set; }
            public string first_name { get; set; }
            public string last_name { get; set; }
            public string email { get; set; }
            public string business_phone { get; set; }
            public string mobile_phone { get; set; }
            public string home_phone { get; set; }
            public string address { get; set; }
            public string address2 { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string zip { get; set; }
            public string country { get; set; }
            public static List<dp_donorsearch> Load(string input)
            {
                List<dp_donorsearch> list = new List<dp_donorsearch>();
                XDocument doc = XDocument.Parse(input);
                var listDict = doc.Descendants("record").AsEnumerable()
                    .Select(x => new
                    {
                        dict = x.Descendants("field")
                            .GroupBy(y => y.Attribute("name").Value, z => z.Attribute("value").Value)
                            .ToDictionary(y => y.Key, z => z.FirstOrDefault())
                    });
                foreach(var dict in listDict)
                {
                    dp_donorsearch newDp = new dp_donorsearch();
                    list.Add(newDp);
                    newDp.donor_id = dict.dict["donor_id"];
                    newDp.first_name = dict.dict["first_name"];
                    newDp.last_name = dict.dict["last_name"];
                    newDp.email = dict.dict["email"];
                    newDp.business_phone = dict.dict["business_phone"];
                    newDp.mobile_phone = dict.dict["mobile_phone"];
                    newDp.home_phone = dict.dict["home_phone"];
                    newDp.address = dict.dict["address"];
                    newDp.address2 = dict.dict["address2"];
                    newDp.city = dict.dict["city"];
                    newDp.state = dict.dict["state"];
                    newDp.zip = dict.dict["zip"];
                    newDp.country = dict.dict["country"];
                }
                return list;
            }

        }
    }
}
​