是否可以从xml创建一个C#类?

时间:2011-09-27 16:40:46

标签: c# xml

是否可以从xml创建一个C#类?

假设我的xml就像,

<Person>
    <Name>aaaa</Name>
    <Email>bbb</Email>
</Person>

然后Person将是类名和名称,电子邮件必须是我的属性。它将如何处理集合或潜在的子对象?

<Person>
    <Name>aaaa</Name>
    <Email>bbb</Email>
    <PhoneNumbers>
        <Number Type="Cell">5555555</Number>
        <Number Type="Home">5555554</Number>
    </PhoneNumbers>
</Person>

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

XML Schema Definition(xsd.exe)工具允许您从XML文件生成公共语言运行时类。可以找到更多信息here

答案 2 :(得分:0)

假设您的意思是xml序列化,您可以使用它作为示例。请注意使用Xml属性来控制序列化/反序列化:

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

namespace ThrowAwayCSConsole
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create an instance of the Person
            Person p = new Person { Name = "John Doe", Email = "jdoe@someisp.com" };
            p.PhoneNumbers.Add(new PhoneNumber { Type = "Home", Number = "999-555-1234" });
            p.PhoneNumbers.Add(new PhoneNumber { Type = "Work", Number = "999-555-9876" });

            StringBuilder output = new StringBuilder();

            XmlSerializer xSer = new XmlSerializer(typeof(Person));

            //serialize it to xml
            using (StringWriter wrt = new StringWriter(output))
            {
                xSer.Serialize(wrt, p);
            }

            //Print the output
            Console.WriteLine(output.ToString());


            //Deserialize the xml string back to an instance of Person
            Person p2 = null;
            using (StringReader rdr = new StringReader(output.ToString()))
            {
                p2 = xSer.Deserialize(rdr) as Person;
            }

            //Use p2 instance here
            Console.WriteLine("\r\nName: {0},  Email: {1}, has {2} phone numbers:", p2.Name, p2.Email, p2.PhoneNumbers.Count);
            foreach (var number in p2.PhoneNumbers)
            {
                Console.WriteLine("    {0}:  {1}", number.Type, number.Number);
            }

            Console.ReadLine();
        }
    }

    [Serializable]
    [XmlInclude(typeof(PhoneNumber))]
    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }

        [XmlArrayItem("Number")]
        public List<PhoneNumber> PhoneNumbers { get; set; }

        public Person()
        {
            PhoneNumbers = new List<PhoneNumber>();
        }
    }

    [Serializable]
    public class PhoneNumber
    {
        [XmlAttribute]
        public string Type { get; set; }
        [XmlText]
        public string Number { get; set; }
    }
}

此代码的输出为:

<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>John Doe</Name>
  <Email>jdoe@someisp.com</Email>
  <PhoneNumbers>
    <Number Type="Home">999-555-1234</Number>
    <Number Type="Work">999-555-9876</Number>
  </PhoneNumbers>
</Person>

Name: John Doe,  Email: jdoe@someisp.com, has 2 phone numbers:
    Home:  999-555-1234
    Work:  999-555-9876

希望这有帮助