Conversion Between XML and List
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Xml;
Namespace XmlHelper
{
/// <Summary>
/// Convert objects to Xml and Xml to object classes
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
Public class XmlHelper <T> where T: new ()
{
# Convert region object class to Xml
/// <Summary>
/// Convert the object instance to xml
/// </Summary>
/// <Param name = "item"> object instance </param>
/// <Returns> </returns>
Public static string EntityToXml (T item)
{
IList <T> items = new List <T> ();
Items. Add (item );
Return EntityToXml (items );
}
/// <Summary>
/// Convert the object instance set to xml
/// </Summary>
/// <Param name = "items"> object instance set </param>
/// <Returns> </returns>
Public static string EntityToXml (IList <T> items)
{
// Create an XmlDocument
XmlDocument doc = new XmlDocument ();
// Create a root element
XmlElement root = doc. CreateElement (typeof (T). Name + "s ");
// Add the child element set of the root element
Foreach (T item in items)
{
EntityToXml (doc, root, item );
}
// Add the root element to the XmlDocument
Doc. AppendChild (root );
Return doc. InnerXml;
}
Private static void EntityToXml (XmlDocument doc, XmlElement root, T item)
{
// Create Element
XmlElement xmlItem = doc. CreateElement (typeof (T). Name );
// Attribute set of the object
System. Reflection. PropertyInfo [] propertyInfo =
Typeof (T). GetProperties (System. Reflection. BindingFlags. Public |
System. Reflection. BindingFlags. Instance );
Foreach (System. Reflection. PropertyInfo pinfo in propertyInfo)
{
If (pinfo! = Null)
{
// Object property name
String name = pinfo. Name;
// Object Property Value
String value = String. Empty;
If (pinfo. GetValue (item, null )! = Null)
Value = pinfo. GetValue (item, null). ToString (); // get the object property value
// Set the attribute value of an element
XmlItem. SetAttribute (name, value );
}
}
// Add a child element to the root
Root. AppendChild (xmlItem );
}
# Endregion
# Region Xml converted to entity class
/// <Summary>
/// Convert Xml into an object instance
/// </Summary>
/// <Param name = "xml"> xml </param>
/// <Returns> </returns>
Public static T XmlToEntity (string xml)
{
IList <T> items = XmlToEntityList (xml );
If (items! = Null & items. Count> 0)
Return items [0];
Else return default (T );
}
/// <Summary>
/// Convert Xml into an object instance set
/// </Summary>
/// <Param name = "xml"> xml </param>
/// <Returns> </returns>
Public static IList <T> XmlToEntityList (string xml)
{
XmlDocument doc = new XmlDocument ();
Try
{
Doc. LoadXml (xml );
}
Catch
{
Return null;
}
If (doc. ChildNodes. Count! = 1)
Return null;
If (doc. ChildNodes [0]. Name. ToLower ()! = Typeof (T). Name. ToLower () + "s ")
Return null;
XmlNode node = doc. ChildNodes [0];
IList <T> items = new List <T> ();
Foreach (XmlNode child in node. ChildNodes)
{
If (child. Name. ToLower () = typeof (T). Name. ToLower ())
Items. Add (XmlNodeToEntity (child ));
}
Return items;
}
Private static T XmlNodeToEntity (XmlNode node)
{
T item = new T ();
If (node. NodeType = XmlNodeType. Element)
{
XmlElement element = (XmlElement) node;
System. Reflection. PropertyInfo [] propertyInfo =
Typeof (T). GetProperties (System. Reflection. BindingFlags. Public |
System. Reflection. BindingFlags. Instance );
Foreach (XmlAttribute attr in element. Attributes)
{
String attrName = attr. Name. ToLower ();
String attrValue = attr. Value. ToString ();
Foreach (System. Reflection. PropertyInfo pinfo in propertyInfo)
{
If (pinfo! = Null)
{
String name = pinfo. Name. ToLower ();
Type dbType = pinfo. PropertyType;
If (name = attrName)
{
If (String. IsNullOrEmpty (attrValue ))
Continue;
Switch (dbType. ToString ())
{
Case "System. Int32 ":
Pinfo. SetValue (item, Convert. ToInt32 (attrValue), null );
Break;
Case "System. Boolean ":
Pinfo. SetValue (item, Convert. ToBoolean (attrValue), null );
Break;
Case "System. DateTime ":
Pinfo. SetValue (item, Convert. ToDateTime (attrValue), null );
Break;
Case "System. Decimal ":
Pinfo. SetValue (item, Convert. ToDecimal (attrValue), null );
Break;
Case "System. Double ":
Pinfo. SetValue (item, Convert. ToDouble (attrValue), null );
Break;
Default:
Pinfo. SetValue (item, attrValue, null );
Break;
}
Continue;
}
}
}
}
}
Return item;
}
# Endregion
}
}
Convert XML to List
List to XML
Convert Model to XML
XML to Model