Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#8

Re: XMLSerializer: Verschachtelte XML-Dateien mit Arrays?

  Alt 4. Feb 2007, 21:17
Zitat von Alexander:
Da gibt es doch sicherlich eine einfache Möglichkeit, oder?
Dich hätte ich fast vergessen...

Du kannst dir ja den Code hier so anpassen dass er mit Delphi.Net oder Chrome/.Net1.1 läuft.
Du benutzt einfach die Klasse CData anstatt String und beim Serialisieren bekommst du eine CData section.

Delphi-Quellcode:
uses
  System.Xml.*;
  
type
  CData = public sealed class(IXmlSerializable,
                              IComparable<String>,
                              IEquatable<String>,
                              IComparable<CData>,
                              IEquatable<CData>)
  public
    class operator Implicit(value : String) : CData;
    class operator Implicit(value : CData) : String;

    class operator Equal(left: CData; right: String) : Boolean;
    class operator Equal(left: CData; right: CData) : Boolean;

    method Equals(obj : Object) : Boolean; override;
    method Equals(obj : CData) : Boolean;
      implements IEquatable<CData>.Equals;
    method CompareTo(obj: CData): Integer;
      implements IComparable<CData>.CompareTo;

    constructor; empty;
    constructor(text : String);
  private
    method Equals(other: String): Boolean;
      implements IEquatable<String>.Equals;
    method CompareTo(other: String): Integer;
      implements IComparable<String>.CompareTo;

    Text : String;

    method WriteXml(writer: XmlWriter);
    method ReadXml(reader: XmlReader);
    method GetSchema: XmlSchema; empty;
  end;
  
implementation

constructor CData(text : String);
begin
  self.Text := text;
end;

method CData.ReadXml(reader: XmlReader);
begin
  Text := reader.ReadString();
end;

method CData.WriteXml(writer: XmlWriter);
begin
  writer.WriteCData(Text);
end;

class operator CData.Implicit(value : String) : CData;
begin
  exit(new CData(value));
end;

class operator CData.Implicit(value : CData) : String;
begin
  exit(value.Text);
end;

method CData.Equals(obj : Object) : Boolean;
begin
  if ReferenceEquals(self, obj)
     or ReferenceEquals(Text, obj) then
    exit(true)
  else if obj is CData then
    with e := CData(obj) do
      exit(Equals(e))
  else if obj is IEquatable<String> then
    with e := IEquatable<String>(obj) do
      exit(e.Equals(Text))
  else if obj is IEquatable<CData> then
    with e := IEquatable<CData>(obj) do
      exit(e.Equals(self));
end;

method CData.Equals(other: String): Boolean;
begin
  exit(Text.Equals(other));
end;

method CData.Equals(obj : CData) : Boolean;
begin
  exit(self = obj);
end;

method CData.CompareTo(other: String): Integer;
begin
  exit(Text.CompareTo(other));
end;

method CData.CompareTo(obj: CData): Integer;
begin
  if not assigned(obj) then
    exit(String.Compare(Text, String(nil)))
  else
    exit(String.Compare(Text, obj.Text))
end;

class operator CData.Equal(left: CData; right: String) : Boolean;
begin
  if not assigned(left)
     and not assigned(right) then
    exit(true)
  else if not assigned(left) then
    exit(String.IsNullOrEmpty(right))
  else
    exit(left.Equals(right));
end;

class operator CData.Equal(left: CData; right: CData) : Boolean;
begin
  if ReferenceEquals(left, right) then
    exit(true)
  else if assigned(left) and assigned(right) then
    exit(String.Equals(left.Text, right.Text))
  else if not assigned(left) then
    exit(String.Equals(right.Text, String(nil)))
  else
    exit(String.Equals(left.Text, String(nil)));
end;
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat