2014-04-16 3 views
1

내부에 많은 큰 클래스가있는 기본 클래스가 있습니다.XML에 직렬화 할 때 속성을 무시합니다.

예를 들어, Person 클래스라고합시다. 그 안에는 Payment 클래스가 있고 내부에 CreditCard 클래스가 있습니다.

클래스 Person을 직렬화하려고하는데, 그 안에 특정 클래스를 제외하고 싶습니다.

이 예제에서는 Person 클래스를 직렬화하고 전체 지불 클래스를 무시하려고합니다. 이것은 지금까지 내가 한 일이지만 작동하지 않습니다.

내가 이걸 어떻게 달성 할 수 있는지 알아 주겠니? 감사합니다.

 XmlAttributes att = new XmlAttributes { XmlIgnore = true }; 
     XmlAttributeOverrides xOver = new XmlAttributeOverrides(); 
     xOver.Add(typeof(Payment), "Payment", att); 
     SerializeContractsRequest(items, xOver); 

public static string Serialize<T>(T clrObject, XmlAttributeOverrides xmlOverrides) where T : class, new() 
{ 
    XmlSerializer xs = xmlOverrides == null ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), xmlOverrides); 
    string xml = string.Empty; 

    //A string builder that will hold the converted business object as an xml string 
    StringBuilder sb = new StringBuilder(); 

    //The stream that will write the serialized xml to the stringbuilder object 
    XmlWriterSettings settings = new XmlWriterSettings(); 
    settings.Encoding = Encoding.UTF8; 

    XmlWriter writer = XmlWriter.Create(sb, settings); 

    xs.Serialize(writer, clrObject); 

    xml = sb.ToString(); 

    return xml; 
} 

또한 Payment 클래스 XML 속성을 터치 할 수 없습니다. 예를 들어 Payment 클래스에 [XmlIgnore]을 추가 할 수 없습니다.

이 방법은 한 가지 방법으로 만 필요하므로 여기에서 재정의를 적용하고 싶습니다. 그러나, 단지 참조를 위해,이 Payment 클래스가 무엇 : 당신이 재정의를 지정하면

[System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] 
    [System.Runtime.Serialization.DataContractAttribute(Name="Payment", Namespace="http://schemas.datacontract.org/2004/07/ServicesLayer")] 
    [System.SerializableAttribute()] 
public partial class Payment 
{ 

} 
+0

http://stackoverflow.com/questions/17947998/ignore-property-of-a-property-in-xml-serialization-in-net-using-xmlserializer – Aybe

+0

그래, 나는 이미 그것을 보았습니다. 나는 단지 재산이 아니라 전체 수업을 무시하고 싶다. 제발 뭘했는지보세요. 작동하지 않습니다. – JohnC1

+1

클래스는 궁극적으로 속성으로 표시됩니다. – Aybe

답변

2

, 당신은 패스에서 속성이 포함 된 유형 :

using System.Collections.Generic; 
using System.IO; 
using System.Windows; 
using System.Xml.Serialization; 

namespace WpfApplication10 
{ 
    public partial class MainWindow 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      Loaded += MainWindow_Loaded; 
     } 

     private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      var person = new Person 
      { 
       Payment = new Payment { Amount = 100 }, 
       Payments = new List<Payment> 
       { 
        new Payment { Amount = 200 }, 
        new Payment { Amount = 400 } 
       } 
      }; 

      var attributes = new XmlAttributes { XmlIgnore = true }; 

      var overrides = new XmlAttributeOverrides(); 
      overrides.Add(typeof(Person), "Payment", attributes); 
      overrides.Add(typeof(Person), "Payments", attributes); 

      var serializer = new XmlSerializer(typeof(Person), overrides); 
      using (var stringWriter = new StringWriter()) 
      { 
       serializer.Serialize(stringWriter, person); 
       string s = stringWriter.ToString(); 
      } 
     } 
    } 

    public class Person 
    { 
     public List<Payment> Payments { get; set; } 
     public Payment Payment { get; set; } 
     public int SomethingElse { get; set; } 
    } 

    public class Payment 
    { 
     public decimal Amount { 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"> 
    <SomethingElse>0</SomethingElse> 
</Person> 
+0

나는 이것을 정확히 한 것처럼 보이지만 여전히 나에게는 아직 효과가 없지만 코드가 완벽하다고 확신한다. 문제는이 프로젝트가 엉망이므로 디버깅을 계속할 것입니다. 고마워. – JohnC1

+0

프로젝트 디버깅에 행운을 비네 : D – Aybe

관련 문제