2009-09-10 33 views

Trả lời

3

Bạn chỉ có thể đặt nó vào được một tài sản byte[] và nó sẽ Base64 mã hóa nó tự động:

public class Test { 
    public byte[] MyProperty {get;set;} 

    public void SetMyProperty(string text) 
    { 
     MyProperty = System.Text.Encoding.Unicode.GetBytes(text); 
    } 
} 

Test test = new Test(); 
test. SetMyProperty("123456789123456789"); 

Output:

<MyProperty>MQAyADMANAA1ADYANwA4ADkAMQAyADMANAA1ADYANwA4ADkA</MyProperty> 

(Hãy thử giải mã rằng here)

Đáng tiếc là có là không có cách nào (mà tôi biết) để làm MyProperty riêng tư và vẫn được đăng trong System.Xml.Serialization.

2

Bạn lưu trữ chuỗi dưới dạng giá trị Base64 và sau đó có thuộc tính giải mã nó trong mệnh đề get.

+1

+1, mặc dù tôi muốn làm điều đó theo chiều ngược lại (lưu trữ đơn giản, có một tài sản mà mã hóa/giải mã nó chỉ cho serialization , miễn là tuần tự hóa ít được sử dụng hơn là chỉ truy cập chuỗi). – OregonGhost

0

Cách duy nhất được hỗ trợ để thay đổi đầu ra từ lớp XmlSerializer (không có dấu hiệu xấu xí như có đặc tính ẩn đặc biệt, v.v.) là triển khai giao diện IXmlSerializable.

Bạn có thể tự lưu mình phải viết mã tuần tự cho toàn bộ lớp bằng cách xác định lớp Base64String triển khai IXmlSerializable và chỉ ghi ra chuỗi được mã hóa. Xác định một toán tử để làm cho nó implicitly castable thành một chuỗi và nó sẽ hoạt động giống như một chuỗi bình thường.

4

Base64 chuyển đổi nhị phân dữ liệu vào chuỗi. Nếu bạn muốn base64 mã hóa dữ liệu trong một chuỗi, trước tiên bạn cần phải mã hóa dữ liệu trong mảng byte, ví dụ: sử dụng Encoding.UTF.GetBytes(myString).

Điều này đặt ra câu hỏi về lý do chính xác bạn muốn thực hiện điều này ngay từ đầu. Nếu bạn cần sử dụng cơ sở 64, bạn có chắc chắn rằng bạn đã thực sự có dữ liệu văn bản để bắt đầu không?

+1

Vâng, có các chuỗi ký tự '" "' và '" \ 0 "' tồn tại một chuyến đi vòng tới/từ XML theo mặc định sẽ là tốt đẹp. – binki

4

Theo Jon Grant gợi ý hữu ích Tôi đã triển khai loại Base64String đóng gói mã hóa Base64 bắt buộc.

public class Base64String: IXmlSerializable 
{ 
    private string value; 

    public Base64String() { } 

    public Base64String(string value) 
    { 
     this.value = value; 
    } 

    public string Value 
    { 
     get { return value; } 
     set { this.value = value; } 
    } 

    public static implicit operator string(Base64String x) 
    { 
     return x.ToString(); 
    } 

    public static implicit operator Base64String(string x) 
    { 
     return new Base64String(x); 
    } 

    public override string ToString() 
    { 
     return value; 
    } 

    #region IXmlSerializable Members 

    public System.Xml.Schema.XmlSchema GetSchema() 
    { 
     return null; 
    } 

    public void ReadXml(System.Xml.XmlReader reader) 
    { 
     MemoryStream ms = null; 
     byte[] buffer = new byte[256]; 
     int bytesRead; 

     while ((bytesRead = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0) 
     { 
      if (ms == null) 
       ms = new MemoryStream(bytesRead); 

      ms.Write(buffer, 0, bytesRead); 
     } 

     if (ms != null) 
      value = System.Text.UnicodeEncoding.Unicode.GetString(ms.ToArray()); 
    } 

    public void WriteXml(System.Xml.XmlWriter writer) 
    { 
     if (!string.IsNullOrEmpty(value)) 
     { 
      byte[] rawData = Encoding.Unicode.GetBytes(value); 
      writer.WriteBase64(rawData, 0, rawData.Length); 
     } 
    } 

    static public string EncodeTo64(string toEncode) 
    { 
     byte[] toEncodeAsBytes 
       = System.Text.UnicodeEncoding.Unicode.GetBytes(toEncode); 
     string returnValue 
       = System.Convert.ToBase64String(toEncodeAsBytes); 
     return returnValue; 
    } 

    static public string DecodeFrom64(string encodedData) 
    { 
     byte[] encodedDataAsBytes 
      = System.Convert.FromBase64String(encodedData); 
     string returnValue = 
      System.Text.UnicodeEncoding.Unicode.GetString(encodedDataAsBytes); 
     return returnValue; 
    } 

    #endregion 
} 

Và bạn có thể sử dụng các loại như thế này:

static void Main(string[] args) 
{ 
    Foo foo = new Foo(); 
    foo.Field1 = "Pluto"; 
    foo.Field2 = "Pippo"; 
    foo.Field3 = "Topolino"; 
    foo.Field4 = "Paperino"; 

    XmlSerializer ser = new XmlSerializer(typeof(Foo)); 
    ser.Serialize(Console.Out, foo); 
    Console.ReadLine(); 
} 

[XmlRoot("Sample")] 
public class Foo 
{ 
    public Foo() { } 

    [XmlElement("Alfa_64")] 
    public Base64String Field1; 

    [XmlElement("Beta")] 
    public string Field2; 

    [XmlElement("Gamma_64")] 
    public Base64String Field3; 

    [XmlElement("Delta_64")] 
    public Base64String Field4; 
} 
Các vấn đề liên quan