添加与其他类型相同的名称的XmlElement

时间:2014-08-07 20:55:25

标签: c# xml

我在XML文件中有结构:

<Employee>
    <EmpId>1</EmpId>
    <Name>Sam</Name>
    <Phone Type="Home">423-555-0124</Phone>
    <Phone Type="Work">424-555-0545</Phone>
</Employee>

和班级:

public class Phone
{
    [XmlAttribute("type")]
    public string Type { get; set; }
    [XmlText]
    public string Value { get; set; }
}
public class Employee
{
    [XmlElement("EmpId")]
    public int Id { get; set; }

    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("Phone", ElementName = "Phone")]
    public Phone phone_home { get; set; }

    [XmlElement("Phone2", ElementName = "Phone")]
    public Phone phone_work { get; set; }

    public Employee() { }
    public Employee(string home, string work)
    {
        phone_home = new Phone()
        {
            Type = "home",
            Value = home
        };
        phone_work = new Phone()
        {
            Type = "work",
            Value = work
        };
    }
    public static List<Employee> SampleData()
    {
        return new List<Employee>()
        {
            new Employee("h1","w1"){
                Id   = 1,
                Name = "pierwszy",
            },
            new Employee("h2","w2"){
                Id   = 2,
                Name = "drugi",
            }
        };
    }
}

但我的问题是我无法添加名为“Phone”的两个XmlElement。当我尝试编译它然后我有两个相同名称的XmlElement异常(重复:电话)。我该如何解决?

3 个答案:

答案 0 :(得分:2)

使用此:

[XmlType("Phone")]
public class Phone
{
    [XmlAttribute("type")]
    public string Type { get; set; }
    [XmlText]
    public string Value { get; set; }
}

[XmlType("Employee")]
public class Employee
{
    [XmlElement("EmpId", Order = 1)]
    public int Id { get; set; }

    [XmlElement("Name", Order = 2)]
    public string Name { get; set; }

    [XmlElement(ElementName = "Phone", Order = 3)]
    public Phone phone_home { get; set; }

    [XmlElement(ElementName = "Phone", Order = 4)]
    public Phone phone_work { get; set; }

    public Employee() { }
    public Employee(string home, string work)
    {
        phone_home = new Phone()
        {
            Type = "home",
            Value = home
        };
        phone_work = new Phone()
        {
            Type = "work",
            Value = work
        };
    }

public static List<Employee> SampleData()
    {
        return new List<Employee>()
    {
        new Employee("h1","w1"){
            Id   = 1,
            Name = "pierwszy",
        },
        new Employee("h2","w2"){
            Id   = 2,
            Name = "drugi",
        }
    };
    }
}

序列化代码:

var employees = Employee.SampleData();

System.Xml.Serialization.XmlSerializer x = 
new System.Xml.Serialization.XmlSerializer(employees.GetType());

x.Serialize(Console.Out, employees);

这是你的结果:

<?xml version="1.0" encoding="windows-1250"?>
<ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employee>
    <EmpId>1</EmpId>
    <Name>pierwszy</Name>
    <Phone type="home">h1</Phone>
    <Phone type="work">w1</Phone>
  </Employee>
  <Employee>
    <EmpId>2</EmpId>
    <Name>drugi</Name>
    <Phone type="home">h2</Phone>
    <Phone type="work">w2</Phone>
  </Employee>
</ArrayOfEmployee>

答案 1 :(得分:0)

您在[XmlRoot]课程中使用Phone属性。 [XmlRoot]定义文档的根,并且xml文档中只能有一个根元素。您的Employee课程应该具有基于您向我们展示的xml的[XmlRoot]属性。

Phone课程应该没有属性,只有Employee.Phone课程的Employee成员,就像你现在一样。

答案 2 :(得分:0)

考虑使用属于List对象的属性替换Employee类上的工作和家庭电话属性。这更灵活(如果你最终支持其他类型而不是工作或家庭),。net xml序列化知道如何处理它。

请参阅Is it possible to deserialize XML into List<T>?

只需将phone_home和phone_work属性替换为:

[XmlElement("Phone"]
public List<Phone> Phones { get; set; }

序列化的xml应与您在上面指定的相同。