【问题标题】:How to implement Custom XML Serialization in C#如何在 C# 中实现自定义 XML 序列化
【发布时间】:2012-11-21 13:45:53
【问题描述】:

下面是我的 XML 文件。

<Employee>
    <FirstName>#{FirstName}#</FirstName>
    <LastName>#{LastName}#</LastName>
    <DOB>#{DOB}#</DOB>
    <Address>#{Address}#</Address>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>

我的可序列化类是

[XmlRoot("Employee"), Serializable]    
 public class Employee    
 {    
     [XmlAnyElement]
      public List<XmlElement> EmployeeDetails    { get; set; }
 }

但我想得到这样的东西

在我的 EmployeeDetails 中,我应该只序列化 FirstName、LastName、DOB、Address ......并且我应该在一个单独的类中获取交易列表,其中包含 Month 和 Amount 作为可序列化的元素。

类似的东西

[XmlRoot("Employee"), Serializable]    
public class Employee    
{    
 [XmlAnyElement]
 public List<XmlElement> EmployeeDetails    { get; set; }

 [XmlElement("Transcation")]
 public List<Transcation> Transcations { get; set; }

}

public class Transcation
{
  [XmlElement("Month")]
    public string Month{ get; set; }
  [XmlElement("Amount")]
    public string Amount{ get; set; }

}

我该怎么做?

【问题讨论】:

    标签: c# xml-serialization datacontractserializer


    【解决方案1】:

    假设您无法修改 XML,您的类应该如下所示。可以使用 XmlAnyElement,但是您可能需要将其设置为对象数组。所以你的代码应该是这样的:

    [XmlRoot("Employee"), Serializable]
    public class Employee
    {
        [XmlAnyElement]
        public XmlElement [] EmployeeDetails { get; set; }
    
        [XmlElement("Transcation")]
        public List<Transaction> Transcations { get; set; }
    }
    

    要反序列化,请执行以下操作:

        private void DeserializeObject(string filename)
        {
            XmlAnyElementAttribute myAnyElement = new XmlAnyElementAttribute();
            XmlAttributeOverrides xOverride = new XmlAttributeOverrides();
            XmlAttributes xAtts = new XmlAttributes();
            xAtts.XmlAnyElements.Add(myAnyElement);
            xOverride.Add(typeof(Employee), "EmployeeDetails", xAtts);
    
            XmlSerializer ser = new XmlSerializer(typeof(Employee), xOverride);
    
            FileStream fs = new FileStream(filename, FileMode.Open);
            var g = (Employee)ser.Deserialize(fs);
            fs.Close();
    
            Console.WriteLine(g.EmployeeDetails.Length);
            foreach (XmlElement xelement in g.EmployeeDetails)
            {
                Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
            }
        }
    

    我建议改为指定属性,这样可以更轻松地查找特定值。但这取决于您事先对 xml 架构的了解。

    [XmlRoot(ElementName="Employee")]    
    public class Employee    
    {    
      [XmlElement(ElementName="FirstName")]    
      public string FirstName {get;set;}
    
      [XmlElement(ElementName="LastName")]
      public string LastName {get;set;}
    
      [XmlElement(ElementName="DOB")]
      public DateTime DOB {get;set;}
    
      [XmlElement(ElementName="Address")]
      public string Address {get;set;}
    
      [XmlElement(ElementName="Transaction")]
      public List<Transaction> Transaction {get;set;}
    }
    
    
    [XmlRoot(ElementName="Transaction")]
    public class Transaction
    {
    
      [XmlElement(ElementName="Month")]
      public int Month {get;set;}
    
      [XmlElement(ElementName="Amount")]
      public int Amount {get;set;}
    }
    

    【讨论】:

      【解决方案2】:

      如果您想完全控制 XML 的外观,您可以创建自己的 XMLDocument 并手动添加节点/元素。更多的工作,但您可以精确地调整 XML 的外观。

      【讨论】:

        猜你喜欢
        • 2010-12-03
        • 1970-01-01
        • 2012-01-31
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        • 1970-01-01
        • 2012-06-08
        相关资源
        最近更新 更多