I have string in xml format with database table name and column description (like column name and data type) and I need to use it for create model(which will map the table) or modify model according the string at runtime. What is the best solution to do this?
When application is running, I need to access some table, with some columns, that can be changed in time, so i need to adapt model for this table.
Please recommend me the technique via I can reach the working solution. Thanks
My data:
I have this in string var:
<xml>
<col>
<name>OS</name>
<type>string</format>
</col>
<col>
<name>Name</name>
<type>string</format>
</col>
<col>
<name>Number</name>
<type>int</format>
</col>
</xml>
This xml string I obtain after some user action. So, I need to use it for access to table in .NET MVC. I use NHibernate, so I need to have model which map the table(name of the table is contained in my xml string) and then add NHibernate mapping for this model, I think. But I don't know, how to do it by some "clear" way
Related
I have a string that is written out like an XML file. An example would look like this:
string = <Employees><EmployeeId>1</EmployeeId>< ... ></Employees>
I am saving this in a table because I wanted to audit changes, but I didn't want to have multiple tables for different audits. This is because it would record changes to things other than employees. So using an XML style string in the database seemed like a good suggestion.
Now to the real business. I want to check to make sure that there were actually changes to the employee because one could go into the edit page, change nothing, and click save. As of right now, the data would write to the DB and just clutter it up with non-changed data.
I'd like to be able to check if the XML styled string that is going to be saved is on the database, so if <employees><employeeid>###</employeeid> == "changes" and then see if the whole string equals the other. Basically, check the employeeId first because that won't change, and then check the string as a whole to see if there is any difference. I would have just checked the first n numbers, but the id number could have a length of 1 to 3.
Also, because it is styled as XML, is there an easy way to convert it to read it like an XML file and check that way?
Storing arbitrary data in a column is a form of denormalization. You can't really do much with it at a database level. However, SQL Server does have an XML column type. Entity Framework doesn't support mapping to/from an XML column, so it will simply treat your XML as a standard string. With this column type, though, you can write actual SQL queries against your XML using XPath expressions.
Your best bet, then, is to type your column as XML, and then write a stored procedure that performs the query you need. You can then utilize this stored procedure with Entity Framework.
For more information on the XML column type see: https://msdn.microsoft.com/en-us/library/ms190798(SQL.90).aspx
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 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.
I am trying to dump the contents of an xml file in to a sql table (relational format).
In the past, I have used dataset.readxml but have had some issues with it while handling many to many relationships.
So I took an approach as follows, create a unique list of xpath expression for all the elements and attributes in the xml file and then using these expressions create a sql db structure to persist this data.
I wanted to create this feature as a dynamic implementation to handle any xml.
Let's say I have an xml file and the unique Xpath are as follows
/movie/MovieId[1]
/movie/MovieName[1]
/movie/cast[1]
/movie/cast[1]/name[1]
/movie/cast[1]/name[2]
/movie/cast[1]/name[3]/#rolename
/movie/description[1]
/movie/directors[1]
/movie/directors[1]/name[1]
/movie/directors[1]/name[1]
/movie/directors[1]/name[2]
/movie/directors[1]/name[3]
/movie/releasedate[1]
/movie/releasedate[1]/#reldatetestatt
/movie/runtime[1]
I would like the table structure to be created as follows:
TableName: Movie
Columns: MovieId, MovieName, description, releasedate, reldatetestatt, runtime
TableName: Cast
Columns: MovieId, name, rolename
TableName: Directors
Columns: MovieId, name
Sample Xml for your reference:
var xml = #"<movies>
<movie>
<MovieId>277345</MovieId>
<MovieName>The Life and Passion of Jesus Christ</MovieName>
<description>The Gospels of the New Testament.</description>
<runtime>44</runtime>
<releasedate reldatetestatt='testattribute'>3/26/1904 12:00:00 AM</releasedate>
<directors>
<name>Lucien Nonguet</name>
<name>Ferdinand Zecca</name>
<name>Ferdinand Zecca 2</name>
</directors>
<cast>
<name rolename='test'>Madame Moreau</name>
<name>Monsieur Moreau</name>
</cast>
</movie>
</movies>";
You could try Talend Open Studio (aka Jaster ETL). It will happily take xml and fill a database. You can do it from the UI or export the "job" as independent java code.
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)