04.06.2009, 23:05 | #1 |
Участник
|
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:
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 <, > & ' and " 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, напишите личное сообщение администратору. |
|