parse text file using xml template - c#

Not quite sure how to word this. Looking for info on VB.net or C#, either one will do.
I'm trying to make a generic file parser that will use an XML file to define the file layout (field names, data types, delimiter type, file type, etc).
The idea is to use this in programs that have differing input file layouts and types (delimited/flat file) without having to modify and recompile. Would like it this way as occasionally the local offices change the format (add/remove fields) and it breaks production until the code is recompiled, re-tested etc.
Unfortunately, as I said, I have no idea how to even look this up.
Any help pointing me in the right direction would be appreciated.

I'd consider to change input file to be xml (or to have corresponding xml).
In this case - you can create your xsd schema, and validate it using xsd.
You can even send this xsd to your offices to validate there inputs before they send it to you.
One other way it to use SSIS

You can use for that an open source solution called FinTp.

Related

Change Document template path in multiple Word Templates

I have about 500 word documents that I need to change the server name in the Document template path. I am not an expert with VBA but I have tried several solutions that have not worked for me. Is there a way to do this (perhaps with C#, with a foreach loop on the directory?) that I can do a very simple find and replace on this field ?
i.e.
\\ASDCFS\NtierFiles\...
becomes
\\NewServer\NtierFiles\...
You can't write to the field in the dialog box directly. In the object model the equivalent is Document.AttachedTemplate and yes, you can work with that. Using in the object model (whether using VBA or C#) you'd loop the documents in a folder, open each in Word, assign the correct path, save and close.
More efficient and less prone to "hiccups" if the original template path is already invalid, would be to edit the documents' Word Open XML directly, without using the Word application. The Open XML SDK would be a good tool for this. It provides the AttachedTemplate class (https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.attachedtemplate(v=office.14).aspx).
You can use WTC to correct the template path in a bulk of documents. You find the source code and binary on Github: https://github.com/NeosIT/wtc

How to convert Visio document .xml to csv file c#

I have multiple xml files, this files generated from convert .vsdx "Visio File" to .xml file, Now i want to convert the generated files from .xml to .csv files.
My problem the files has hundreds lines and the xml shape tag can't be followed to extract csv file using xml tag.
I work with this tool but the problem the out structure very complex
there is any way to make that ?
Normally one solves this with XSLT. If you want to create a csv file programmatically I would query the xml file with Linq to Xml append each record as a csv formatted line into a StringBuilder instance and finally write the StringBuilders content into the target file.
I haven't worked with Visio based XML files but given that the XML'ishness is alike across any source (which it should be), I'd go LINQ2XML or, depending on what you feel convenient with read in the stuff as a XDocument (avoid XmlDocument, as it's older and obsolete) and then parse it to a String.
It depends a bit on how complex files are and how invasive operations that you'll need to carry out.
As for the tool you mention, I haven't seen it before but it could be a better idea not to use it as it strikes me as a bit outdated. However, I only glanced at it without scrutinizing. If you only need to see the data for yourself (human based analysis of the contents), you might perhaps use a decent text editor with some appropriate plugin (such as Notepad++ and its XML add-on).

C# My own file format

I'm looking to make my own file format .
that format should contains pictures/pdf/and other files ...
also need to know how I can packer-unpacker for this format to unpack files from it/pack in it & reading the pictures from my own format to picture boxes on my WinForm for example.
I've searched but didn't really found what I'am looking for
I hope someone can help me , thank you
Zip is an excellent choice. Because you can encrypt the file and of course reduce the file size in some cases (text and uncompressed things). But if you want to create your own file format you can easily decide rules for your storage and order inside the file. Then serialize the info into the file. For example by object serialization or by writing the binary date to file object by object .
if you really want to write your own file format then I would suggest one of two things. One, you could do it entirely in binary at which point you would want to do a 'chunk' format. Chunk format is to basically have a header to each subsection. The header contains the size of both the header as well as the size of the payload. Create a serialization class for your header then add the bytes to the filestream from your payload. Actually pretty easy to do.
Second (and easier) way to do this would be to create an XML format. Create a master class for your format then add all of the data as collections of sub classes under that. Once you have that, use any of .net xml serialization classes to serialize it out to disk.
You can also use SQLite for your purposes. It provides dbms power without needing server. That is popular solution for your problem.
System.Data.SQLite is an ADO.NET adapter for SQLite.

XML Database in C#.net

I am developing a WPF client program for some websites. It uses XML database. I am new to XML. Would someone please explain how to create,append(Most important),edit,read&encrypt XML file. It is a big question,i know . But, it is urgent.Have to complete the work ASAP. Searched in the internet, not getting correct info.
You should seriously consider using a DataSet within your application and load up your data from an XML file via DataSet,ReadXml. When you're done with your updates write your changes using DataSet.WriteXml.
But you should also seriously consider not using XML as a database.
Here's an article on CodeProject that discusses using XML as a database:
Xml Database Demo
I know you tagged this question C# but unfortunately the demo app is written in VB.NET.
(in response to your comment on Gerri's answer)
XML is inherently not appendable. A valid XML document requires a single document element. In order to "append" you would need to be able to back over the closing tag of the document element and overwrite it. The only option is to read in the entire document and write it back out again. Also you may want to use XmlDocument or XDocument instead of XmlWriter which is a horribly painful API when you don't need very fine grained control over the behavior.
The fact is, XML makes a really terrible database format. There's other lightweight database solutions out there that don't require a database server.
Assuming your database is small enough that you can easily load it into memory.
Create classes that model your database.
Add DataContract attributes to them to indicate how you want them serialized.
Use DataContractSerializer to serialize your database to XML and then save it to disk.
Each time you update the database:
Create new file as .tmp
Delete any old file called .old
Rename .xml to .old
Rename new file from .tmp to .xml
When you go to load the file, if .xml is corrupt or missing, try .tmp
This will help you survive the inevitable corruption that will occur during writing when something goes wrong.
Due to history of each data base company coming up with a "standard" interface that no other company follows, XML has become the defacto way to transfer data between databases.
If this in the intended use than it is fine as it only has to write in this format some times. There is a lot to worry about in writting XML using .NET as it has a lot of ways to forget to finish the writing leaving open tags (always use using/flush/close). Warning: The more processing cores the more often .Net screws up. Use Thread.BeginCriticalRegion()/Thread.EndCriticalRegion() if you have more than four real cores. Also as suggested it is best to save the earlier version as a .bak or such.
Of course if the XML standards could have a declaration of "document set" then we could append a document each time and life would be a lot easier.
Load your XML as an XMLDocument.

C# XmlDocument.LoadXml And Wildcards

I have an asp.net application and I'm using C#. I want to use the XmlDocument.LoadXml() method to read from an .xml file. However, the xml file will not always have the same name so I wanted to pass into the LoadXml() method the path to the file and then read any .xml files that are inside. So, something like this LoadXml(C:\Docs*.xml). It doesn't work for me. Is there another way I can accomplish this?
You need to separate out the "loading XML from a file" from "picking which file to load". The two are unrelated concepts. (Although I would point out that XmlDocument.LoadXml takes raw XML as a string, not a filename. I think you want XmlDocument.Load.)
What do you want to happen if there's more than one XML document in c:\Docs? XmlDocument can only load one of them.
Use Directory.GetFiles(#"C:\Docs", "*.xml") to get the list of matching files in the directory. What you should do if there's more than one of them (or none) is up to you.

Categories

Resources