2014-08-27 39 views
0

我正在使用XMLIgnore屬性在序列化中刪除不需要的屬性。但我想只從子類中刪除一些基類屬性。我想要基類的屬性,但它不應該在子類節點中重複。XMLIgnore:從子類節點中刪除基類屬性

是否可以從子類節點中刪除基類屬性?

在我的代碼中,我得到的輸出格式如下:當我通過XMLIgnore從基類中刪除屬性。

<?xml version="1.0" encoding="utf-8"?> 
<InformationCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <projects> 
    <Project xsi:type="Group"> 
     <GroupName>Accounts</GroupName> 
     <Comment>Financial Transaction</Comment> 
    </Project> 
    </projects> 
</InformationCollection> 

但下面格式實際上我期待輸出

<?xml version="1.0" encoding="utf-8"?> 
<InformationCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <projects> 
     <ProjectId>1</ProjectId> 
     <ProjectName>HRMS</ProjectName> 
     <Project xsi:type="Group"> 
     <GroupName>Accounts</GroupName> 
     <Comment>Financial Transaction</Comment> 
    </Project> 
    </projects> 
</InformationCollection> 

我試圖這樣下面的代碼:

[XmlInclude(typeof(Group))] 
    public class Project 
    { 
     public int ProjectId { get; set; } 
     public string ProjectName { get; set; } 
     public Project() { } 
     public Project(int projectId, string projectName) 
     { 
      ProjectId = projectId; 
      ProjectName = projectName; 
     } 
    } 
    public class Group : Project 
    {   
     public string GroupName;   
     public string Comment; 
     public Group():base() { } 
     public Group(int projectId, string projectName) 
      : base(projectId, projectName) 
     { 

     } 
     public Group(int projectId, string projectName, string groupName, string comment) 
      : this(projectId, projectName) 
     { 
      GroupName = groupName; 
      Comment = comment; 
     } 
    } 
    public class InformationCollection 
    { 
     public List<Project> projects = new List<Project>(); 
     public InformationCollection() 
     { 
      projects.Add(new Group(1,"HRMS","Accounts","Financial Transaction")); 
     } 
    } 


    class Program 
    { 
     static void Main(string[] args) 
     { 
      SerializeObject("IgnoreXml.xml"); 
     } 

     public static XmlSerializer CreateOverrider() 
     { 
      XmlAttributeOverrides xOver = new XmlAttributeOverrides(); 
      XmlAttributes attrs = new XmlAttributes(); 
      attrs.XmlIgnore = true; 
      xOver.Add(typeof(Project), "ProjectName", attrs); 
      xOver.Add(typeof(Project), "ProjectId", attrs); 
      XmlSerializer xSer = new XmlSerializer(typeof(InformationCollection), xOver); 
      return xSer; 
     } 

     public static void SerializeObject(string filename) 
     { 
      try 
      { 
       XmlSerializer xSer = CreateOverrider(); 
       InformationCollection informationCollection = new InformationCollection();     
       TextWriter writer = new StreamWriter(filename); 
       xSer.Serialize(writer, informationCollection); 
       writer.Close(); 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 
     } 
    } 

回答

0

假設你有兩個基本的控制和派生類,你可以通過爲每個要爲其控制輸出的屬性XXX添加虛擬bool ShouldSerializeXXX()方法,通過XmlSerializer執行此操作。在基類中,make方法應該返回true,並且在派生類中,它應該被覆蓋以返回false

例如,假設你想抑制只有一個派生類中Id屬性的序列化,你可以這樣做:

public class BaseClass 
{ 
    public int Id { get; set; } 

    public virtual bool ShouldSerializeId() 
    { 
     return true; 
    } 
} 

public class DerivedFromBaseClass : BaseClass 
{ 
    public override bool ShouldSerializeId() 
    { 
     return false; 
    } 
} 

,然後進行測試:

public static void TestSuppressingPropertyInDerivedClass() 
    { 
     var baseClass = new BaseClass() { Id = 31 }; 
     var derivedClass = new DerivedFromBaseClass { Id = 31 }; 

     var baseXml = baseClass.GetXml(); 
     // Xml looks like 

     Debug.Assert(baseXml.Contains("Id")); // No assert 
     var derivedXml = derivedClass.GetXml(); 
     Debug.Assert(!derivedXml.Contains("Id")); // no assert. 
    } 

輸出XML對於BaseClass看起來像:

<?xml version="1.0" encoding="utf-16"?> 
<BaseClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Id>31</Id> 
</BaseClass> 

而對於DerivedFromBaseClass

<?xml version="1.0" encoding="utf-16"?> 
<DerivedFromBaseClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /> 

正如你所看到的,Id已經在XML僅用於派生類,這是壓抑,我想,你是什麼要求。

但是,我認爲你的對象模型可能並不理想。如果基類中的屬性不應出現在派生類中,則表明您應該提取更一般的抽象基類:

public abstract class AbstractProject { 
{ 
    // Properties common to "Group" and "Project" 
} 

public class Project : AbstractProject { 
{ 
    // Properties specific to Project 
} 

public class Group : AbstractProject { 
{ 
    // Properties specific to Group 
}