2011-02-28 114 views
3

我有一个类,如下所示:XML序列化

class SomeClass 
    { 
     private int _property1; 
     [XmlAttribute("Property1")] 
     public int Property1 
     { 
      get { return _property1; } 
      set { _property1 = value; } 
     } 
     private int _property2; 
     [XmlAttribute("Property2")] 
     public int Property2 
     { 
      get { return _property2; } 
      set { _property2 = value; } 
     } 

     private string _property3; 
     public string Property3 
     { 
      get { return _property3; } 
      set { _property3 = value; } 
     } 

     public SomeClass() 
     { 
     } 
    } 

我需要在以下格式序列化使用XmlSerializer的是:

<SomeClass Property1="NNNNN" Property2="NNNNN"> 
    Value_of_Property3 
</SomeClass> 

但是,我不能图我如何序列化Property3的值,而无需为Property3添加节点。是否有一个序列化Property3中的字符串而不添加节点?

+0

“但是,我不知道如何在没有为Property3添加节点的情况下序列化Property3的值。” - 请问为什么? – Smur 2011-02-28 17:58:55

+0

@Felipe Fiali - 可能是安全。经常被序列化的数据是人类无法读取的。 – clamchoda 2011-02-28 18:30:28

+0

@Chris是的,但XML序列化并非如此。如果你想序列化人类形式无法读取的数据,你经常使用二进制序列化。另外,什么额外的安全性将序列化属性作为文本给你? – Smur 2011-03-01 16:41:22

回答

4

[XmlText()]属性添加到Property3

2
[XmlText] 
public string Property3 
{ 
    get { return _property3; } 
    set { _property3 = value; } 
} 
+1

谢谢队友! XmlText正是我所需要的 – IraqiGeek 2011-02-28 18:45:48

+0

@IraqiGeek你应该标记他的答案是正确的。 – clamchoda 2011-03-01 20:00:14