AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 04.06.2009, 23:05   #1  
Blog bot is offline
Blog bot
Участник
 
25,617 / 848 (80) +++++++
Регистрация: 28.10.2006
X++: Serializing Axapta Foundation Class instances as XML streams
Источник: http://blogs.msdn.com/x/archive/2009...l-streams.aspx
==============

The Axapta foundation classes are a set of generally applicable classes that contain values of either simple types or other classes. They are:

  • Structs, structures of named fields,
  • Arrays, arrays of any type, not just simole type
  • Lists, lists allowing traversal with enumerators
  • Maps, dictionaries allowing lookup of values, yielding a result value
  • Sets, ordered groups where duplicates cannot occur.
The classes are generally well understood, so this is not going to be a tutorial on how to use them. This blog will deal with the infrastructure provided to allow serializing and deserializing values to XML streams, that can then be processed (searched with XPath queries, persisted in files or databases etc) as any other XML document.



In the example below we will be describing how this works for structs, but the same principles apply to the other foundation classes as well.

The struct class has a method that yields an XML representation of its content. Not suprisingly, this method is called Xml(). As you can imagine, this method can easily deal with instances of simple types, but structs are not limited to containing simple types: They can contain instances of any type defined by the user. Ideally we would like these classes to be able to participate in the serialization and subsequent deserialization. As it happens this is exactly what the infrastructure provides, with a little help from the developer.



When the xml method is called on the struct, the implementation will call xml (int indent) on any objects in the struct. It is then up to this implementation to serialize the state of the object into XML. Let's consider an example where a struct contains a user defined Point class, with the obvious set of x,y values. The class, listed below contains an XML method that returns a string containing the XML format of the state of the object. The indent parameter indicates the indentation level; padding with spaces as done in the example below is not really needed: There are no semantics for white space in XML, but it makes the XML document look good in the eyes of carbon based lifeforms.



class Point
{
int x;
int y;

public void new(int _x, int _y)
{
x = _x; y = _y;
}

public str ToString()
{
return strfmt("Point(%1,%2)", x,y);
}

public str xml(int indent=0)
{
return strrep(" ", indent) + strfmt("", x,y);
}
}




If you use string operations like above, you should be careful to not include the XML metacharacters ,&,'," in the values, You should use the <, > & &apos; and &quot; placeholders instead. You can use .NET to build the XML stream for you (it will escape the characters for you) or place content inside [CDATA[]] tags. For the example above this would clearly be overkill.



With this in place, you can serialize the content of a struct into an XML stream and work with it as an XML document:



static void structserializetest(Args _args)
{
struct s = new Struct ("str name; int age; Point p");
XmlDocument d = new XmlDocument();
str xml;



s.value("Name", "John Doe");
s.value("Age", 42);
s.value("Point", new Point(43, 77));


xml = s.xml();

// Load the XML we just serialized into an XML DOM


d.loadXml(xml);

}



But that's not all. If you add a



static public CreateFromXml(XmlNode n)
{
return new Point(
str2int(n.attributes().getNamedItem("x").value()),
str2int(n.attributes().getNamedItem("y").value()));
}




method to the class you will be able to create the point instance from deserializing the XML you just created:



// … continued from example above…

// Load the XML wejust serialized into an XML DOM

d.loadXml(xml);



copy = struct::createFromXML(d.documentElement()); // Serialize



// Show the results. Should match the original value.

print copy.definitionString();

print copy.toString();











Источник: http://blogs.msdn.com/x/archive/2009...l-streams.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Axapta Lessons: Exporting Tables and Class definitions Blog bot DAX Blogs 0 28.10.2006 18:22
AXAPTA 4.0 задерживается до весны 2006 (eng.) dmit2604 Microsoft и системы Microsoft Dynamics 61 12.03.2005 16:14
Говорят вышел SP2 для Axapta 3. Кто нибуть что знает на эту тему? soin DAX: Прочие вопросы 10 13.10.2003 10:43
XML в Axapta axot DAX: Программирование 4 01.11.2002 11:37
Введение в Аксапту Роман Кошелев DAX: Прочие вопросы 0 18.12.2001 14:00

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 04:27.