bauerA
01-04-2012, 13:31
Прошу помочь с переводом следующих фрагментов кода, т.к. с delphi более или менее знаком, а с С# только начинаю:
unit StData;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls, Contnrs, CodeUnit;
type
TStQuestion = class
private
FActive: boolean;
FText: string;
public
StAnswerList: TObjectList;
constructor Create;
property Active: boolean read FActive write FActive;
property Text: string read FText write FText;
end;
TStAnswer = class
private
FRight: Boolean;
FStQuestion: TStQuestion;
FText: string;
public
property Right: Boolean read FRight write FRight;
property StQuestion: TStQuestion read FStQuestion write FStQuestion;
property Text: string read FText write FText;
end;
TStTest = class(TObject)
private
FQCount: Integer;
FText: string;
public
property Text: string read FText write FText;
property QCount: Integer read FQCount write FQCount;
end;
TStData = class(TObject)
private
FStQuestionList: TObjectList;
FStTest: TStTest;
XMLDocument: IXMLDocument;
FFileName: string;
procedure LoadData;
public
procedure Load(setFileName: string);
procedure LoadFromXml(XMLStr: string);
procedure LoadEmpty;
procedure Save;
procedure AppendQuestion( newQuestion: TStQuestion );
procedure DeleteQuestion( delQuestion: TStQuestion );
class function GetInstance: TStData;
property FileName: string read FFileName write FFileName;
property StQuestionList: TObjectList read FStQuestionList;
property StTest: TStTest read FStTest;
strict private
constructor Create;
strict private
class var FInstance: TStData;
end;
implementation
constructor TStQuestion.Create;
begin
StAnswerList := TObjectList.Create;
end;
constructor TStData.Create;
begin
XMLDocument := TXMLDocument.Create( nil );
FStQuestionList := TObjectList.Create;
FStTest := TStTest.Create;
LoadEmpty;
end;
procedure TStData.Load(setFileName: string);
begin
FFileName := setFileName;
XMLDocument.LoadFromXml( TStEncode.GetFile(setFileName) );
LoadData;
end;
procedure TStData.LoadFromXml(XMLStr: string);
begin
XMLDocument.LoadFromXML( XMLStr );
LoadData;
end;
procedure TStData.LoadEmpty;
const
DEFAULT_HEADER = '<?xml version="1.0" encoding="UTF-8"?>' +
'<test text="" qcount="0"></test>';
begin
XMLDocument.LoadFromXML( DEFAULT_HEADER );
LoadData;
FFileName := '';
end;
procedure TStData.AppendQuestion( newQuestion: TStQuestion );
begin
FStQuestionList.Add( newQuestion );
end;
procedure TStData.DeleteQuestion( delQuestion: TStQuestion );
begin
FStQuestionList.Remove( delQuestion );
end;
class function TStData.GetInstance: TStData;
begin
if FInstance = nil then
begin
FInstance := TStData.Create;
end;
Result := FInstance;
end;
procedure TStData.LoadData;
var
i, j: integer;
tmpQuestion: TStQuestion;
tmpAnswer: TStAnswer;
Root: IXMLNode;
Node: IXMLNode;
begin
FStQuestionList.Clear;
Root := XMLDocument.DocumentElement;
FStTest.Text := Root.Attributes['text'];
FStTest.QCount := StrToInt( Root.Attributes['qcount'] );
for i := 0 to Root.ChildNodes.Count - 1 do
begin
tmpQuestion := TStQuestion.Create;
tmpQuestion.Text := Root.ChildNodes[i].Attributes['text'];
tmpQuestion.Active := StrToBool( Root.ChildNodes[i].Attributes['active'] );
Node := Root.ChildNodes[i];
for j := 0 to Node.ChildNodes.Count - 1 do
begin
tmpAnswer := TStAnswer.Create;
tmpAnswer.StQuestion := tmpQuestion;
tmpAnswer.Right := StrToBool( Node.ChildNodes[j].Attributes['right'] );
tmpAnswer.Text := Node.ChildNodes[j].Attributes['text'];
tmpQuestion.StAnswerList.Add( tmpAnswer );
end;
FStQuestionList.Add( tmpQuestion );
end;
end;
procedure TStData.Save;
var
List: TObjectList;
i, j: integer;
tmpAnswer: TStAnswer;
Root: IXMLNode;
Node: IXMLNode;
NodeAns: IXMLNode;
XMlStr: string;
begin
List := FStQuestionList;
Root := XMLDocument.DocumentElement;
Root.Attributes['text'] := StTest.Text;
Root.Attributes['qcount'] := IntToStr(StTest.QCount);
Root.ChildNodes.Clear;
for i := 0 to List.Count - 1 do
begin
Node := Root.AddChild( 'question' );
Node.Attributes['text'] := TStQuestion( List.Items[i] ).Text;
Node.Attributes['active'] := TStQuestion( List.Items[i] ).Active;
for j := 0 to TStQuestion( List.Items[i] ).StAnswerList.Count - 1 do
begin
tmpAnswer := TStAnswer( TStQuestion( List.Items[i] ).StAnswerList.Items[j] );
NodeAns := Node.AddChild( 'answer' );
NodeAns.Attributes['right'] := BoolToStr( tmpAnswer.Right, true );
NodeAns.Attributes['text'] := tmpAnswer.Text;
end;
end;
XMLDocument.SaveToXML( XMlStr );
TStEncode.SaveFile( XMlStr, FFileName );
end;
end.
unit StData;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls, Contnrs, CodeUnit;
type
TStQuestion = class
private
FActive: boolean;
FText: string;
public
StAnswerList: TObjectList;
constructor Create;
property Active: boolean read FActive write FActive;
property Text: string read FText write FText;
end;
TStAnswer = class
private
FRight: Boolean;
FStQuestion: TStQuestion;
FText: string;
public
property Right: Boolean read FRight write FRight;
property StQuestion: TStQuestion read FStQuestion write FStQuestion;
property Text: string read FText write FText;
end;
TStTest = class(TObject)
private
FQCount: Integer;
FText: string;
public
property Text: string read FText write FText;
property QCount: Integer read FQCount write FQCount;
end;
TStData = class(TObject)
private
FStQuestionList: TObjectList;
FStTest: TStTest;
XMLDocument: IXMLDocument;
FFileName: string;
procedure LoadData;
public
procedure Load(setFileName: string);
procedure LoadFromXml(XMLStr: string);
procedure LoadEmpty;
procedure Save;
procedure AppendQuestion( newQuestion: TStQuestion );
procedure DeleteQuestion( delQuestion: TStQuestion );
class function GetInstance: TStData;
property FileName: string read FFileName write FFileName;
property StQuestionList: TObjectList read FStQuestionList;
property StTest: TStTest read FStTest;
strict private
constructor Create;
strict private
class var FInstance: TStData;
end;
implementation
constructor TStQuestion.Create;
begin
StAnswerList := TObjectList.Create;
end;
constructor TStData.Create;
begin
XMLDocument := TXMLDocument.Create( nil );
FStQuestionList := TObjectList.Create;
FStTest := TStTest.Create;
LoadEmpty;
end;
procedure TStData.Load(setFileName: string);
begin
FFileName := setFileName;
XMLDocument.LoadFromXml( TStEncode.GetFile(setFileName) );
LoadData;
end;
procedure TStData.LoadFromXml(XMLStr: string);
begin
XMLDocument.LoadFromXML( XMLStr );
LoadData;
end;
procedure TStData.LoadEmpty;
const
DEFAULT_HEADER = '<?xml version="1.0" encoding="UTF-8"?>' +
'<test text="" qcount="0"></test>';
begin
XMLDocument.LoadFromXML( DEFAULT_HEADER );
LoadData;
FFileName := '';
end;
procedure TStData.AppendQuestion( newQuestion: TStQuestion );
begin
FStQuestionList.Add( newQuestion );
end;
procedure TStData.DeleteQuestion( delQuestion: TStQuestion );
begin
FStQuestionList.Remove( delQuestion );
end;
class function TStData.GetInstance: TStData;
begin
if FInstance = nil then
begin
FInstance := TStData.Create;
end;
Result := FInstance;
end;
procedure TStData.LoadData;
var
i, j: integer;
tmpQuestion: TStQuestion;
tmpAnswer: TStAnswer;
Root: IXMLNode;
Node: IXMLNode;
begin
FStQuestionList.Clear;
Root := XMLDocument.DocumentElement;
FStTest.Text := Root.Attributes['text'];
FStTest.QCount := StrToInt( Root.Attributes['qcount'] );
for i := 0 to Root.ChildNodes.Count - 1 do
begin
tmpQuestion := TStQuestion.Create;
tmpQuestion.Text := Root.ChildNodes[i].Attributes['text'];
tmpQuestion.Active := StrToBool( Root.ChildNodes[i].Attributes['active'] );
Node := Root.ChildNodes[i];
for j := 0 to Node.ChildNodes.Count - 1 do
begin
tmpAnswer := TStAnswer.Create;
tmpAnswer.StQuestion := tmpQuestion;
tmpAnswer.Right := StrToBool( Node.ChildNodes[j].Attributes['right'] );
tmpAnswer.Text := Node.ChildNodes[j].Attributes['text'];
tmpQuestion.StAnswerList.Add( tmpAnswer );
end;
FStQuestionList.Add( tmpQuestion );
end;
end;
procedure TStData.Save;
var
List: TObjectList;
i, j: integer;
tmpAnswer: TStAnswer;
Root: IXMLNode;
Node: IXMLNode;
NodeAns: IXMLNode;
XMlStr: string;
begin
List := FStQuestionList;
Root := XMLDocument.DocumentElement;
Root.Attributes['text'] := StTest.Text;
Root.Attributes['qcount'] := IntToStr(StTest.QCount);
Root.ChildNodes.Clear;
for i := 0 to List.Count - 1 do
begin
Node := Root.AddChild( 'question' );
Node.Attributes['text'] := TStQuestion( List.Items[i] ).Text;
Node.Attributes['active'] := TStQuestion( List.Items[i] ).Active;
for j := 0 to TStQuestion( List.Items[i] ).StAnswerList.Count - 1 do
begin
tmpAnswer := TStAnswer( TStQuestion( List.Items[i] ).StAnswerList.Items[j] );
NodeAns := Node.AddChild( 'answer' );
NodeAns.Attributes['right'] := BoolToStr( tmpAnswer.Right, true );
NodeAns.Attributes['text'] := tmpAnswer.Text;
end;
end;
XMLDocument.SaveToXML( XMlStr );
TStEncode.SaveFile( XMlStr, FFileName );
end;
end.