Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use fnSimpleObjectToXML in VB.NET

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use fnSimpleObjectToXML in VB.NET", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use fnSimpleObjectToXML in VB.NET" this article.

1. I name this semi-automatic initialization file class clsAPPOption. The structure of the class and the function of the process are described as follows:

The contents in the two areas # Region "definition of option structure used by the application" and # Region "variable declaration of application options" are classes and instances of classes organized by initialization option level. Each class represents an initialization section of the program option, which must be manually supplemented by the end user according to the actual choice.

◆ fnGetAppDirectory: get the running directory of the application

◆ sbInitialDefaultAPPOption: sets the default initial value for program options (the code in this process needs to be manually modified according to actual needs).

◆ fnSaveAppOption: saves the program options in the class to a specified file. (some of the code in this process needs to be modified manually according to the actual needs.)

◆ fnLoadAppOption: reads the application's option information in the specified file and saves it to the current class (some of the code in this process needs to be manually modified as needed).

◆ fnXMLElementToSimpleObject: turn a XMLElement into the simple object it represents, which is a basic net object such as int32\ int16.

◆ fnXMLElementToClassObject: converts a XMLElement to the class object it represents.

◆ fnXMLElementToArray: converts a XMLElement to the array it represents.

◆ fnArrayToXML: put an array into a XML file. Currently, only one-dimensional arrays are supported: for example, dim aString (10) as string

◆ fnClassObjectToXML: converts a class object to a representation of a xml element.

◆ VB.NET fnSimpleObjectToXML: change a simple object to a XML element representation

◆ Note: where fnXMLElementToXXXX and fnXXXXToXML functions correspond to each other and are inverse functions.

2, the implementation of the program is very simple, fnClassObjectToXML reflects the specified class, enumerates the member variable information in the class using the GetFields method of the class type, and then calls VB.NET fnSimpleObjectToXML or fnArrayToXML according to the type of the member variable. At the end of the function, the class to be converted is returned in the form of a XMLElement object.

For Each tobjFieldInfo In tobjClassObjectType.GetFields

If tobjFieldInfo.FieldType.IsArray = False Then'is just a simple type that gets the value directly

TobjXMLElement = fnSimpleObjectToXML (tobjFieldInfo.GetValue (ni_objClassObject), _

Ni_objXMLDocument, _

TobjFieldInfo.Name)

TobjXMLClassObjectElement.AppendChild (tobjXMLElement)

Else

If REM is an array type, make a call to the array method to get the value

'currently, only one-dimensional array elements are supported

TobjXMLElement = fnArrayToXML (tobjFieldInfo.GetValue (ni_objClassObject))

Ni_objXMLDocument, tobjFieldInfo.Name, tobjFieldInfo.FieldType.FullName)

'put the array object in the structured xml object

TobjXMLClassObjectElement.AppendChild (tobjXMLElement)

End If

The implementation of VB.NET fnSimpleObjectToXML is also very simple. According to the definition of the simple object established earlier, several elements of the xml object to be generated by the VB.NET fnSimpleObjectToXML process can be obtained as follows: ObjectName has been obtained after f nClassObjectToXML is reflected and provided in the call to VB.NET fnSimpleObjectToXML by passing parameters. The ObjectType data type can be obtained using the ni_objSimpleObject.GetType.FullName method, and ni_objSimpleObject is the value of the simple object passed in when the function is called.

There is a slight problem when writing the fnArrayToXML procedure, because it is impossible for the caller to require the elements of the array to be passed in one by one, so the number of elements and the value of the elements of the array cannot be directly obtained by object reflection in the function. Fortunately, in the Net reflection operation, you can use the Invoke method to call functions or attribute procedures in the original object, and the array sharing methods GetLength and GetValue function well to take the number of array elements and specify the element value of the subscript, so the problem is solved immediately.

'use reflection to call the getLenght method of the array to get the size of the array. Only one-dimensional arrays are supported here. But tobjXMLAttribute = ni_objXMLDocument.CreateAttribute ("ArrayLength") tobjMethodInfo = tobjArrayType.GetMethod ("GetLength") ReDim taobjParameter (0) taobjParameter (0) = 0 ti32Tempa = tobjMethodInfo.Invoke (ni_objArray) is sufficient for initialization files TaobjParameter) tobjXMLAttribute.Value = ti32Tempa tobjXMLElement.SetAttributeNode (tobjXMLAttribute) tobjXMLAttribute = Nothing 'elements in the array For ti32LoopA = 0 To ti32Tempa-1' using the GetValue square method of the reflection array to obtain the value of the actual element ReDim taobjParameterType (0) taobjParameterType (0) = GetType (Integer) tobjMethodInfo = tobjArrayType.GetMethod ("GetValue", taobjParameterType) ReDim taobjParameter (0) taobjParameter (0) = ti32LoopA tobjTempa = tobjMethodInfo.Invoke (ni_objArray TaobjParameter) 'put array elements into the xml object tobjXMLElementA = fnSimpleObjectToXML (tobjTempa, ni_objXMLDocument, ti32LoopA) tobjXMLElement.AppendChild (tobjXMLElementA) Next ti32LoopA

There is little technical difficulty in the process of moving from XML to net objects. I simply get the type of simple object according to the ObjectType information contained in xml, and then directly call the function of net cast class to restore the stored value of the object specified in the XML file to the original net object. The code is as follows:

According to the object type specified in the XMLelement structure element, create the object tobjType = Type.GetType (tobjXmlSimpleObjectElement.GetAttribute ("ObjectType")) tobjReturnSimpleObject = Convert.ChangeType (tobjXmlSimpleObjectElement.InnerText, tobjType, Nothing) these are all the contents of the article "how to use fnSimpleObjectToXML in VB.NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report