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.
Related
I have XML objects that hold form data from a web application. These are as simple as can be for an xml format, a demonstrative example would be:
<data>
<patientFirstName>Bob</patientFirstName>
<patientLastName>Bobson</patientLastName>
</data>
and unfortunately due to humans beyond my control, roughly 150 other potential fields for the current "document".
The primary goal is to transfer this data from XML to a more "human-friendly" format such as a spreadsheet or PDF, etc..
I have tried using a DataSet as the source for a DataGrid and producing an html table. This works for a browser, and technically loads in Excel - but Excel gives a file type error, as the file is not in Excel format, it is literally an HTML table.
Is there any way to use the .net framework, or other MS objects whether managed or (preferably) unmanaged to take XML or a DataSet and turn it into a useable file format by typical office applications?
Is there a way to use SSRS without SQL queries or files? My big issue is this is a small Information System that does many things dynamically. I have seen examples of creating Excel spreadsheets using a file on the disk...but it is 2019 - I will allow my code to create or read files, the concept of a file is quite obsolete and a major source of security dilemmas.
In a perfect world I would want to create a decent looking report form template and simply bind certain nodes to their respective node in the XML. This would seem like something that many people would want to do, and as if it would be easy to implement, but search as much as I may and I find 99 terrible ways to pretend to make an Excel file and not 1 decent one. SSRS would seem like a lovely option if I could request to use a premade template and pump XML into it or a DataSet from a middleware server - though my issue is that it seems to be dumbed-down to the point it is designed to do ALL the work in some not-very-flexible manner.
If all else fails, might there be some MS functionality for pagination that I can use to create my own mini-report generator? I feel I may be forced to..
Please understand I am not looking to hand-parse markup in C# - in unmanaged C++ I would consider it, but C# becomes very slow when you start doing things "by hand" as opposed to using objects and their methods.
.net 4.5+
I want to have pointer to xml file and when ever i need to read from him some info i will go directly to the place in this xml file and bring the fresh info.
How can i hold this xml ?
I need to give the ability to change the info in this file also.
You can use the FileSystemWatcher class to detect whenever the XML file gets modified (and react by reloading it). The example on MSDN is quite instructive.
You can't really have a "pointer to xml file" as such. I suggest using XmlReader and XmlWriter classes for reading / writing the XML if the file is expected to be rather big, or LINQ to XML and the XElement class if its size is more likely to be moderate.
If you come across any particular implementation problems, specify them.
I have two TextRanges from two different RichTextBoxes, and four strings from regular textboxes. I would like to save all this information in one file, and then be able to load it later. Whats the best approach?
I've been reading some about it, and it seems that reading all into one memorystream and then save it to a file is one way to do it. And then parse this content later.
Anyone that want to share some experience, and simple code?
For a simple approach consider creating a class with string properties for each of your textbox texts. You could then set the properties when you want to save your text, use XML serialization to save the class to an XML formatted file, and then read it back at a later time.
The advantage of this approach is that you will not need to hande low-level file handling or parsing yourself.
Searching for C# and XML Serialization will yield plenty of code examples.
One solution is already provided by you: save in file and read after.
Another could be, in case if the data is too big, is using http://msdn.microsoft.com/en-us/library/dd997372.aspx Memory Mapped Fies.
I'm fairly new to coding, and I just got help figuring out how to create a Xml file; now I want to know, is there a way to protect my Xml file from being edited?
I'm making a simple Command Prompt game, and I'm going to include an Xml file for info storage purposes. Although I don't want the user to be able to change the info contained in the file.. Is there a way to achieve this? It doesn't need to be extensive at this time, due to the program only being a small project.
Anyway, I'm making the program with Visual Studio Pro 2010, and I'm coding it in C#.
Thank you, for any help in advance.
the standard way to verify that parts of your xml has not been modified is to use XML_Signature
this msdn example shows how this is done with dotnet4
I would embed your XML file as a resource of your console application's assembly. The XML file will exist as an embedded resource and not as a seperate file that the user could potentially change. If the user isn't meant to edit a configuration file, don't even let him see it, modify it, or delete it.
look at this topic decrypt and encrypt
i have created my own Encrypter class based from this classes. then you can create it for yourself for next use
You could simply compress it, if you don't need a high level of security. You could use a standard format (ZIP, CAB), or just deflate the stream and store it as a binary file. See the doc and examples about this here: DeflateStream Class
You can't prevent anyone from editing your xml file but you can encrypt your xml file to protect your data.
We store a word document in an Oracle 10g database as a BLOB object. I want to read the contents (the text) of this word document, make some changes, and write the text alone to a different field in a C# code.
How do I do this in C# 2.0?
The easiest logic that I came up with is this -
Read the BLOB object
Store it in the FileSystem
Extract the text contents
Do your job
Write the text into a separate field.
I can use Word.dll but not any commercial solutions such as Aspose
I assume that you already know how to do steps 1 and 2 (use the Oracle.DataAccess and System.IO namespaces).
For step 3 and 5, use Word Automation. This MS support article shows you how to get started: How to automate Microsoft Word to create a new document by using Visual C#
If you know what version of Word it will be, then I'd suggest using early binding, otherwise use late binding. More details and sample code here: Using early binding and late binding in Automation
Edit: If you don't know how to use BLOBs from C#, take a look here: How to: Read and Write BLOB Data to a Database Table Through an Anonymous PL/SQL Block
This keeps coming up in my searches, so I'll add an answer for the benefit of future readers.
I highly recommend avoiding Word automation. It's painfully slow and subjects you to the whims of Microsoft's developers with each upgrade. Instead, process the files manually yourselves if you can. The files are nothing but zipped archives of XML files and resources (such as images embedded in the document).
In this case, you'd simply unzip the docx using your preferred library, manipulate the XML, and then zip the result back up.
This does require the use of docx files rather than doc files, but as the link above explains, this has been the default Word format since Office 2007 and shouldn't present an issue unless your users are desperately clinging to the past.
For an example of the time savings, Back in 2007 we converted one process that took 45 minutes using Word automation and, on the same hardware, it took 15 SECONDS processing the files manually. To be clear, I'm not blaming Microsoft for this - their Word automation methods don't know how you will manipulate the document, so they have to anticipate and track everything that you could possibly change. You, on the other hand, can write your method with laser focus because you know exactly what you want to do.