Reading XML Content

Posted on Microsoft .NET Support Team See other posts from Microsoft .NET Support Team
Published on Wed, 08 May 2013 12:57:00 +0000 Indexed on 2013/06/24 16:32 UTC
Read the original article Hit count: 169

Filed under:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Diagnostics;
using System.Threading;
using System.Xml;
using System.Reflection;

namespace XMLReading

{

class Program
    {
static void Main(string[] args)
        {

string fileName = @"C:\temp\t.xml";
List<EmergencyContactXMLDTO> emergencyContacts = new XmlReader<EmergencyContactXMLDTO, EmergencyContactXMLDTOMapper>().Read(fileName);

foreach (var item in emergencyContacts)
            {
Console.WriteLine(item.FileNb);
            }
         }
    }

public class XmlReader<TDTO, TMAPPER> where TDTO : BaseDTO, new() where TMAPPER : PCPWXMLDTOMapper, new()
    {
public List<TDTO> Read(String fileName)
        {
XmlTextReader reader = new XmlTextReader(fileName);
List<TDTO> emergencyContacts = new List<TDTO>();
while (true)
            {
                TMAPPER mapper = new TMAPPER();
bool isFound = SeekElement(reader, mapper.GetMainXMLTagName());
if (!isFound) break;
                TDTO dto = new TDTO();
foreach (var propertyKey in mapper.GetPropertyXMLMap())
                {
String dtoPropertyName = propertyKey.Key;
String xmlPropertyName = propertyKey.Value;
                    SeekElement(reader, xmlPropertyName);
                    SetValue(dto, dtoPropertyName, reader.ReadElementString());
                }
                emergencyContacts.Add(dto);
            }
return emergencyContacts;
        }

private void SetValue(Object dto, String propertyName, String value)
        {
PropertyInfo prop = dto.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
            prop.SetValue(dto, value, null);
        }

private bool SeekElement(XmlTextReader reader, String elementName)
        {
while (reader.Read())
            {
XmlNodeType nodeType = reader.MoveToContent();
if (nodeType != XmlNodeType.Element)
                {
continue;
                }

if (reader.Name == elementName)
                {
return true;
                }
            }
return false;
        }
    }

public class BaseDTO

    {

    }

public class EmergencyContactXMLDTO : BaseDTO
    {

public string FileNb { get; set; }

public string ContactName { get; set; }

public string ContactPhoneNumber { get; set; }

public string Relationship { get; set; }

public string DoctorName { get; set; }

public string DoctorPhoneNumber { get; set; }

public string HospitalName { get; set; }

    }

public interface PCPWXMLDTOMapper
    {
Dictionary<string, string> GetPropertyXMLMap();
String GetMainXMLTagName();
    }

public class EmergencyContactXMLDTOMapper : PCPWXMLDTOMapper
    {
public Dictionary<string, string> GetPropertyXMLMap()
        {
return new Dictionary<string, string>
            {
                { "FileNb", "XFileNb" },
                { "ContactName", "XContactName"},
                { "ContactPhoneNumber", "XContactPhoneNumber" },
                { "Relationship", "XRelationship" },
                { "DoctorName", "XDoctorName" },
                { "DoctorPhoneNumber", "XDoctorPhoneNumber" },
                { "HospitalName", "XHospitalName" },
            };
        }

public String GetMainXMLTagName()
        {
return "EmergencyContact";
        }
    }
}

© Microsoft .NET Support Team or respective owner