I need to generate a report in csv format. Data is retrieved as List<T> and only certain columns needs to be displayed in the report. I am thinking of doing it by storing its schema in an XML document under App_Data folder and use LINQ to XML to retrieve the field names and create the report.
Sample XML:
<report>
<fields>
<field headercaption='Customer Name'>CName</field>
<field headercaption='Address'>CAddress</field>
...
</fields>
</report>
Is it advisable to completely depend on XML file this way or do I need to do it through coding.
Edit
Fields are properties of List<T> which needs to be populated in the CSV files. Header caption is the name of the column for the field in the report.
Sample Report
Customer Name, Address,
ALFKI , 31 Independence Ave., Washington
If you just want to add/remove columns at run time in this one report, it would be even easier to use a plain text file. Store column information in a format you can easily parse ( something like CName|Customer Name). When you need to generate CSV, read all lines, split by | (or whatever separator you choose) and go from there. You will probably also want to add a comment or documentation note somewhere about the format (in case you or someone else need to come back and modify it in a year or more).
XML might become useful if you:
plan to put several reports into one file, and maybe give them names
provide some way for other people to submit their own reports
create some authoring tool to allow people to customize reports
localize column names in CSV. You could define something like
.
<report>
<fields>
<field name="CName">
<header language="en-us">Customer Name</header>
<header language="es-es">Nombre de cliente</header>
...
</field>
...
</fields>
</report>
want to experiment with XML libraries
In those cases XML may be useful because you could define an XSD and easily validate that submitted reports are valid.
Related
I have an XML file in which I store data about a list of persons and another one in which I store a list of objects like this.
people.xml
<People>
<Person>
<Name>itsName</Name>
<Age> itsAge </Age>
<RecentAcquisitions>
<Acquisition>
<name>Apple</name>
<quantity>5</quantity>
</Acquisition>
</RecentAcquisitions>
</Person>
</People>
objects.xml
<Objects>
<Object>
<Name>Apple</Name>
<Description>Fresh Apple</Description>
<Price>10</Price>
<etc>..lots of attributes..</etc>
</Object>
</Objects>
What is the most efficient way of extracting information from objects.xml based on the person Acquisition List at the runtime? (in example the person should have 5 objects of type "Apple").
Momentarily I use a solution which consists of storing each object from objects.xml in a list and when I'm loading a person I search for the respective object based on Acquisition->Name and add it in the person.AquisitionList;
Is there another way of doing this?
Maybe I misunderstood the XML role but it feels wrong to store the information from an XML file in a list or array at runtime.
to my knowledge, using the runtime memory instead of constant read-write operations is the best way to do it / what you're doing is the right way.
XML can be seen as 2 things:
1 - A way to store information, much like a database, until it needs to be retrieved for processing at runtime
this is what you are doing now... you store the objects list on disk using XML, and then you retrieve it for processing/load it into memory at runtime.
2 - A standardized way of passing information around, regardless of technology.
XML can be read in a multitude of languages and any language that can read a string can technically read and extract the data from an XML document.
I am developing some software that generates reports from event data collected from some devices. The data is stored in a database. I want the user to be able to template the output filename of the report generated by the software. So when the user decides to generate a report, the output filename template they have configured is applied to the data retrieved from the database.
For example, lets say that each report has the following data template elements associated with it:
<Name>
<Timestamp>
<Location>
<EventType>
So the user could select any of these template items in any order to determine the format of the output filename. If they chose something like this:
<Name> <EventType> <Location> <Timestamp>.pdf
And lets say this is the data for the current report in the database:
Name = "MyReport"
EventType = "Error"
Location = "Park Ave"
Timestamp = 1-30-2013 11.00.00 AM
The report filename would look like this when the actual report is generated:
"MyReport Error Park Ave 1-30-2013 11.00.00 AM.pdf"
I'm struggling to come up with an elegant way of providing custom format elements like <Name>, <Location> etc... and building a string from data in a database or possibly an entity object. I have looked at ICustomFormatter and IFormatProvider, but I'm not sure if these are completely appropriate for what I'm doing.
I am using WPF 4.5 and my data will be driven by the Entity Framework.
Any ideas would be appreciated!
I saw this in my questions and that no one ever answered. Just for posterity I wanted to record what I did. I ended up using Regex.Replace. I used an enumeration to list the different format fields like this:
String pattern = String.Format("<{0}>", Template.Timestamp);
String result = Regex.Replace(pattern, data.Timestamp.ToString());
Where "Template" is an enumeration and "Timestamp" is the element I wanted to replace. It seemed to work for my needs.
I have a nested XML document and I am looking at the plausability of using DataSets to parse it.
<?xml version="1.0" standalone="yes"?>
<Workbench>
<Overrides>
<Override name='firstoverride' value='overridevalue'/>
</Overrides>
<DataSets>
<BASIC>
<MEMBNO>1</MEMBNO>
<PERSONNO>0</PERSONNO>
</BASIC>
</DataSets>
</Workbench>
What i want to be able to do is essentially access the contents of the Overrides and DataSets as if there were an actual Dataset.
So to validate I check the root element is workbench.
Then I check to see if there are any overrides, I then want to be able to iterate around the Override Items.
Following that, and this is the hard part I want to support abitary but well form XML that will be inserted into a database but the parsing code can make so assumptions about the data as I want it to be generic.
I can do this if I make the DataSets the root element and iterate around it but it doesn't seem to work if nested?
hlep!
I have a design related question. So please guide me on how to do this?
Right now, I have a xml structure which will be in this format.
<Sheet id="abc"/>
<Elements>
<Element id="xyz">
<value>23</value>
</Element>
<Element id="sdz">
<value>46</value>
</Element>
...
</Elements>
</Sheet>
So, we have a class like this to store each & every element of the sheet.
Sheet
{
Public string SheetId
{
get; set;
}
//all Elements will be stored in the below collection
Public IList<IDictionary<string, string>> Elements
{
get; set;
}
}
But now for few sheets the format has been changed to the below structure.
<abc> //SheetId
<Elements>
<Record>
<Element1/>
<Element2/>
<Element3/>
<Element4/>
</Record>
<Record>
<Element1/>
<Element2/>
<Element3/>
<Element4/>
</Record>
...
</abc>
So, we need to create a Generic class to hold the above xml formats and we don't want to have different object to store these.
I mean in future if we have some other xml format also we need to accommodate the same without any change in the Sheet class.
So, can you please advise me on how to design the Sheet Class.?
Ok. let me explain how my app works.
Actually we have around 200 sheets(in other words measures).
1) User will upload the sheet data in xml format (all sheets in xml file) & edit the same if they want Or Enter the data in the screen (dynamic screen generated using the xml template) if they dont want to upload.
2) Then the data will be stored in the Sheet object and it will go through lot of Validation process and finally the data will be converted to xml again and stored in the db.
You can ask why you want to store this as XML? The reason is we dont want to create 200 aspx pages for this same kind of data and thats why we are generating the sheet pages dynamically using the xml templates. Also, we will be adding, updating or deleting sheets frequently.
Ok. I think now you will have some idea about this issue.
To be more clear, all the elements in my XML file will be displayed as a field in the aspx page. It maybe a Textbox, dropdown & etc....
I would recommend designing your class based on what the information actually represents and how your software plans to utilize the data, not the XML format being used.
You should always be able to do the transposition from the data format into the structure which best represents how your program will use this data.
how can i map xml file(s) of different formats to a table in my database say for example i have a table called Person
FirstName nvarchar(50),
LastName nvarchar(50),
Address nvarchar(500)
i have to map xml files submitted by users to the fields of Person, the xml files can be in different formats say for example one user submits in the following format
<Person>
<Names>
<FirstName>john</FirstName>
<LastName>smith</LastName>
</Names>
<Names>
<FirstName>john</FirstName>
<LastName>smith</LastName>
</Names>
</Person>
and another user submits it in the following format
<Person>
<PersonalInfo>
<Names>
<FirstName>john</FirstName>
<LastName>smith</LastName>
</Names>
.
.
.
<PersonalInfo>
</Person>
how can i device a solution that will handle different cases (different xml formats), so that if a user submits a xml file in whatever format i just have to tell the format to my aplication and its values will automatically gets mapped to the said table... im using visual studio 2010 as an IDE and C#.NET, i hope have narrated the problem well enough...
please advice...
Since I don't how dynamic this should be (meaning how often you will change the mappings) or how many different XMLs do u have I will suggest you to map the table(s) to a common class using your preferred ORM and create class instances using LinqToXml (which is very easy). One method or class per XML structure.
There is a sample here for the LinqToXml -> http://www.switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing
Also, if you donĀ“t want to use it and are using .NET 4 I created a simple library to produce dynamic objects from a XML string. You can take a look at it here -> https://github.com/tucaz/XmlToObjectParser
you can identify the schema you are acquiring and use appropriate XSLT File to convert it to the latest version.
Note: you should make something generic to identify the schema (e.g. )
(I Like 1. better) create an Software Adapter interface that changes the mappings (Like add-ins work)