C# XmlDocument.LoadXml And Wildcards - c#

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.

Related

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).

Design implementation : Uploading XML file and evaluation of predefined path

I have a .xml file, which is the result of an export from a non-relational database.
In my application, as a configuration, a user should be able to select nodes from a xml structure, which would be compared/evaluated later on when he upload the .xml.
I have thought to 2 options, but am not very pleased with both:
option1: the structure of the .xml file has to be stored in the database, so the application can display a TreeView, and the user would simply select the nodes to inspect.
(cons: I have many .xml files, one for each system it relates too and each .xml is quit large)
option2: the user would have to upload a .xml file before starting his configuration, so that its structure would be dynamically generated.
(cons: User has one more step to do in order to make his configuration. The one .xml file that he uploads may not contains all the nodes it could have)
Or maybe there are different ways than displaying a TreeView for that purpose ? As it is hard for me to think out of the box, I can't see other options.
I hope this is clear enough,
Maybe there is a kind of best practices I have missed, I am open to suggestions.
I would go with an intermediate option.
Generate dynamically the xsd schemes corresponding to your xml files and store them somewhere.
Process all the xml files in order to have the most complete xsd, because you said that each xml may not contain the whole structure.

parse text file using xml template

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.

How to hold XML file and get the fresh info from him every time i need

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.

How to parse web.config file in c#

I have web.config file from some application. It is located in some random location. I have to parse this web.config file (get all keys names and values). I tried to use ConfigurationManager class in order to get those data however, it throws exception when I try to get some Sections (Configuration->GetSection('section name')). It throws exception because I do not have dll that this section points to (because I have only web.config not whole application). It seems that GetSection method check underlying dll in order to get more info, but I just need value (name of dll).
What can I do, to turn off this mechanism, do you know other simple solutions to get it done ?
You are just going to have to use XmlDocument or XDocument (3.5) to parse the file.
If you just want to read the text, and not do any web.config-specific processing, use the fact that a .config file is XML, and use your favourite usual way of reading and parsing XML.
Web.Config files are just XML and an be read using a number of .Net XML objects. Below are a couple of methods.
Tutorial on reading an XML file using XmlTextReader http://support.microsoft.com/kb/307548
Tutorial on reading an XML file using LinqToSQL http://www.mssqltips.com/tip.asp?tip=1524

Categories

Resources