Introduction

The XML standard now has become widely used nowadays.However,there're still many people use XML API to deal with XML.This is very trivial and error-prone.This article shows how to intrigrate XML function in new project or migrate an old project smoothly.

Part I. Reading And Wring a XML

.NET introduce a class named XmlSerializer which can serialize a object to xml and vice versa.Let's measure about a XML document.It must contain a root element,which maps to the XmlRoot attribute in .NET.A XML document may contain element,attribute,text etc.In turn they maps to XmlElement,XmlAttribute,XmlText class in .NET framework.Look at the following graph,we use it for the demo:

First let's implement the serialization Corp class.We need to create a instance of XmlSerializer,then use a XmlTextWriter to serialize it.

When .NET process the serialization,it will process the public access member only,either field or property is OK.If you do not want the member serialized,add a XmlIgnore .NET attribute.By default,the serialization will treat the member as element ,so if you wanma add a attribute, you should add a XmlAttribute .NET attribute.Also,the serialization take the member name as the element name or attribute name,so if you want to change the name,you should change the name in the .NET attribute declaration as the above demo. And this also means that,if you add or remove some elements,you don't need to change any code ,this is a advantage compare to traditional method.The most interesting thing (see red underline code) is that you can also delcare the array as a element directly.

The XML comes out with a default namespace,some people prefer a null space,so you can change the code as below.Also,if you want to change the namespace of the xml,just change the namespace parameter.All of .NET XML Attribute constructor have many parameters which is convient for you to change to corresponding xml attribute.

Also,you can use the WriteRaw menthod to write anything you want to write.

We can all use xml for dump instead of write a toString method for every class,this is expecailly useful for debuging.Also we can also dump member of a class as a xml,all we need to do is using the XmlRootAttribute, here is the code:

Part III. Editing a XML File

There're chances that people want to edit a xml file.First,we need a temp object for editing,if the changes are accept,then set the member to the temp object otherwise just discard the temp object.The question is,how to clone a object?Copy fields by fields is awsome.Even with the memberwiseClone menthd of a object,we'all still have a lot of things to do.Fortunatelly,the .NET framework offer a generic SerializeClone method which can do a generic member copy,it just like like Java's serialize.All we need to do is add a .NET [Serializable] for every class,just like the one below:

[Serializable]
publicclass Corp
{
……
};

After that,you can use the following generic SerializeClone method to clone a copy:

Let's think about the editing again,all we need to to is AUD,that's add,update and delete.Here,the add means add a new member to a array,and delete means remove a member from a array.With the help of reflection and generic,this target can simply achieved.

The idea of using array for xml array is rather straight,howerver,we can use another method.

The xml serialization is using public fields or properties.If we use properties declaration,we can have a much more easy way.First we declare a generic list,then we declare the corresponding set & get method.

This is an easy way and you don't need add a Serializable attribute to the class.Through the GetDeptPersonList function,you can manipulate the Person elements easily.And this is the suggest way for manipulate the XML array elements.

Using the code

By using the XMLUtil class,you can serialize a object to a file,or to a string object and vice versa.

// Serialize a object to a file,if the useNullNameSpace parameter is true,then the object will be serialized with a empty namespace.
publicstaticint WriteXML(String xmlFileName, Object oData,bool useNullNameSpace)
// If you want to read a xml from file and deserialize it to an object
publicstaticObject ReadXML(String xmlFilename, Type dataType)
publicstatic T ReadXML<T>(String xmlFilename)
// If you just want to get the XML text of the object,then you can call the GetXML method.
publicstaticString GetXML(Object oData)
// If you want to get the XML text of the object and you want a xsl description or you don't want a empty namespace
publicstaticString GetXML( Object oData,String xslURL,bool useNullNameSpace)
// You may want to get part of the object as xml,you can do this
publicstaticString GetShortXML(Object oData, String strType)
// You may want to deserialize a xml string or a byte array to an object
publicstaticObject LoadXML(String s, Type dataType)
publicstaticObject LoadXML(byte[] bRawData, Type dataType)
publicstaticObject LoadXML<T>(String s)

Some people may to to modify the XML manually,then you can use the ArrayUtil class to archive this target or you can utilize the technique use in the article above:

Points of Interest

Save/Load/Maintenace the XML in a lazy way.

The source comes from SmartK Application Server,which is a fast xslt development platform.

History

Sep 26th,2006 Publish the first version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.