Set fields dynamically in linq in xml Expression using C # in Razor app

I am trying to export an XML file from a database table using XElement. I am using EF 6.0 Code first.

I am preparing XML using XElement, below code below

 TooltypeXml = 
    new XElement("ToolTypes", 
                    (from tbl in db.ToolType
                     where tbl.CreationDate >= objLastSyncByDevice.LocaltoServerLastAccessDate 
                            || tbl.LastModifieDate >= objLastSyncByDevice.LocaltoServerLastAccessDate
                            || tbl.IsDeleted == true
                     select new 
                     { 
                        tbl.ToolTypeId, 
                        tbl.ToolTypeName, 
                        tbl.Action, 
                        tbl.UpdatedBy, 
                        tbl.CreationDate, 
                        tbl.CreatedBy, 
                        tbl.LastModifieDate, 
                        tbl.IsDeleted 
                     }).ToList()
                       .Select(x => 
                            new XElement("ToolType", 
                                new XElement("ToolTypeName", x.ToolTypeName), 
                                new XElement("Action", x.Action), 
                                new XElement("UpdatedBy", x.UpdatedBy), 
                                new XElement("CreationDate", x.CreationDate), 
                                new XElement("CreatedBy", x.CreatedBy), 
                                new XElement("LastModifieDate", x.LastModifieDate), 
                                new XElement("IsDeleted", x.IsDeleted))));

      

So it creates XML format successfully, I want to be able to write my Linq expression so I don't need to list all fields in the expression to select. Since I always need all the fields from the table, and if I change something in the table, I don't need to change anything in the code. Please help. Thank you in advance.

+3


source to share


2 answers


I got a solution to this problem, I added the following code to get all properties of a class and create each field as an XElement.

 private void CreateXElemetsOfClass(System.Type typeOfClass, string TableName, dynamic objData, string p_mainElementName, string MachineID)
        {
            try
            {
                if (objData != null && objData.Count > 0)
                {
                    System.Reflection.PropertyInfo[] properties = typeOfClass.GetProperties();
                    List<XElement> lstXElements = new List<XElement>();
                    List<XElement> lstmainElements = new List<XElement>();
                    XElement rootElement = new XElement(TableName);

                    foreach (var item in objData)
                    {
                        lstXElements = new List<XElement>();
                        XElement mainElement = new XElement(p_mainElementName);
                        foreach (System.Reflection.PropertyInfo property in properties)
                        {
                            var notMapped = property.GetCustomAttributes(typeof(NotMappedAttribute), false);
                            if (notMapped.Length == 0)
                            {
                                lstXElements.Add(new XElement(property.Name, property.GetValue(item, null)));
                            }
                        }
                        mainElement.Add(lstXElements);
                        lstmainElements.Add(mainElement);
                    }
                    rootElement.Add(lstmainElements);

                    string XMLFilePath = serializetoxmlNew(rootElement, MachineID, TableName);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

      

Here the type System.Type classType = typeof(FileTypes);

, objData is the list I want to get in XML, p_mainElementName = "FileType".



It will generate XML as shown below,

<?xml version="1.0" encoding="utf-8"?>
<FileTypes>
  <FileType>
    <FileTypeId>1b254bc3-7516-4f9c-89ea-d9b20ecbf005</FileTypeId>
    <Description>Excel</Description>
    <CreatedBy>22a8be24-9272-4d7e-9248-e9064f917884</CreatedBy>
    <UpdatedBy>22a8be24-9272-4d7e-9248-e9064f917884</UpdatedBy>
    <CreateDate>2014-08-22T16:05:53.177</CreateDate>
    <UpdateDate>2014-08-22T16:05:53.177</UpdateDate>
    <FileExtension>.xls</FileExtension>
    <FileTypeImage>excel.png</FileTypeImage>
    <Action>Create</Action>
    <IsDeleted>false</IsDeleted>
  </FileType>
  <FileType>
    <FileTypeId>f3362487-d96e-4cc8-bc4b-5120866f95ce</FileTypeId>
    <Description>Adobe Acrobat Reader</Description>
    <CreatedBy>22a8be24-9272-4d7e-9248-e9064f917884</CreatedBy>
    <UpdatedBy>22a8be24-9272-4d7e-9248-e9064f917884</UpdatedBy>
    <CreateDate>2014-08-22T16:05:12.407</CreateDate>
    <UpdateDate>2014-08-22T16:05:12.407</UpdateDate>
    <FileExtension>.pdf</FileExtension>
    <FileTypeImage>pdf.png</FileTypeImage>
    <Action>Create</Action>
    <IsDeleted>false</IsDeleted>
  </FileType>
</FileTypes>

      

Hope this helps you! It works great.

0


source


It looks like you're better off using serializing your objects to XML and not creating a custom serialization function.



0


source







All Articles