I've always assumed XML documents are a convenient way to store information. Now I've found in XML a good way to "instruct" my application. Delicous.
My problem is integrate the XML parsing in application classes. I'm using C# System.Xml interface, I think it's good and portable (right?).
Is there any standard interface which defines methods to organize recursion on xml tags, or methods to implement common xml implementations (maybe in a base class)?
Initially I can think to write an interface which defines
void Read(XmlReader xml);
void Write(XmlREader xml);
What what about nested tags, common tags and so on...
P.S.: I don't think to implement this using LINQ, except in the case it's supported also in Mono (how to determine this)?
Thank you very much! :)
I think you might be looking for Serialization, this is a beginners Tutorial on Serialization
As Binary Worrier mentioned, XML serialization is a simple and efficient option. You can also use Linq to XML, which is supported in Mono since version 2.0 (released in october 2008)
Using xml to "instruct" your app seems backwards to me. I'd be more inclined to use an IronPython script if that was my aim. Xml, normally, is intended to serialize data. Sure you can write a language via xml, but ultimately it is fighting the system. You would also massively struggle to invoke methods (easy enough to set properties etc via XmlSerializer, though).
Here's A 3 minute guide to embedding IronPython in a C# application to show what might, IMO, be a better way to "instruct" a C# application via a separate script file.
Related
I would like to know if there is any simple/fast way to create a complex XML file, when I say complex, I mean more than 20 elements encapsulated. Although this is VB.net, unfortunately literals will not work in this case. Some advice will be most certainly appreciated.
You definitely want to System.Xml.Linq tools from .Net 3.5. Even if you aren't using Linq at all, and not using XML literals, it's still a fantastic library for dynamically building XML in code. But since you say you can't use XML literals, does that mean you're in a .Net 3.0 or before project, and you can't upgrade? That would be very unfortunate, to not be able to use the best tooling.
Assuming you can use System.Xml.Linq, and you just have a silly boss who is against the XML literals syntactic sugar (some sort of language snob perhaps?), then you need to get familiar with the library, centered around the XElement class.
I would strongly suggest watching the following video casts on dnrtv, Part1 and Part2.
XLinq: Create XML from object using LINQ
To play with the XML data if you are using .net version 3.5 its better to user LINQ to XML.
http://www.codeproject.com/Articles/24376/LINQ-to-XML
or
Manipulate XML data with XPath and XmlDocument (C#)
In C++ we can import msxml DLL to our application and then implement the methods of the interface ISAXContentHandler in a class of our application. Later create an ISAXReader object and invoke the contentHandler with the instance of this class where we have given the implemnatation for the eventhandler interfaces.
Similiarly I want to achieve the same thing in C#. Is there any way to do this in C#?? I mean is there any way to have eventbased SAX parsing in C#.
There is one more method I thought would help me in achieving parsing the XML documant and getting the XML information to my application.
I followed the link "http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c4221"
Here DOM is used and System.XML namespace is being used.
Can anyone kindly help me with this regard,----What is the way to achieve Event driven XML parsing in C#
or should I use System.XML namespace (DOM). if there exists a SAX event driven way plz let me know how to do it in C#
I think the closest you get to a SAX-reader in the .Net framework is the XmlReader class. It is not event based, but it allows you to iterate through the document in a similar manner. Look at this example: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read.aspx
If you need maximum performance then the XmlReader is a good choice, but there are other alternatives that offer a more friendly api. The XmlDocument class allows you to treat the xml document as a hierarchical tree, while the XDocument allows you to use LINQ to parse the xml. Which one you need depends on which model you are comfortable with and your requirements.
I ran into a similar problem some time ago. While I can't answer which is better, the already implemented System.XML DOM or a C# SAX parser (this really depends on your application), I can tell you that it is indeed possible to implement a SAX parser in C#.
In fact, it has already been done by Martin Friedrich at CodeProject: Towards a Declarative SAX Framework : Part 1 - A Simple SAX-to-C#-Mapping.
I have this XML file, and I want to deserialize it to an object. But I don't want to type its class definition. There is any tool that can create the C# code of the class for me, inferring the data types from sample data?
Yes. Out of the box, you can use xsd.exe to generate XSD files from XML. You can also use this tool to generate classes from XSD files.
The code it produces is limited, which is why there are some third party tools that have stepped in.
Two of those tools include LiquidXML (costs money) and CodeXS (free). We use CodeXS, because it is free and extensible. We have extended it quite a bit.
EDIT:
CodeXS has an online tool. Just give it an XSD. It produces your classes for you.
They also have a command-line tool (source code) which is extensible and doesn't require you to send the XSD to their web service. We use it as a pre-build step.
Liquid Technologies has a good tool for this purpose (Data binding) http://www.liquid-technologies.com/. You'll really need to define a schema though instead of letting such a tool "infer" it from sample data.
One of the benefits of Liquid that we've found is that it can also generate code for Java, C++, C#, VBA etc. All very consistent.
Check out LINQ-to-XSD
It requires that you write a schema for your XML but then it's pretty good about a direct translation to objects.
What are the best functions, practices, and/or techniques to read/write XML with C#/.NET?
If you are working with .NET 3.5, LINQ to XML is certainly a very good way to interact with XML.
MSDN Link
There are classes to read XML:
XmlDocument is slow and memory-intensive: it parses the XML and loads it into an in-RAM DOM, which is good if you want to edit it.
XmlReader is less memory-intensive: it scans the XML from front to back, never needing to keep all of it in RAM at once.
Similarly, for writing you can construct an XmlDocument and then save it, or use an XmlWriter.
After I wrote the above, there's now a new set of APIs which are easier to use: i.e. for example the XDocument and XElement classes.
By far the simplest method I've found for dealing with XML in C# is to use the XML Serialization tools. For example: http://www.dotnetjohn.com/articles.aspx?articleid=173.
Essentially, you can define C# classes that match your XML file (in fact, you can have them created for you if you have an XML definition file) and then you simply initialize instances of those classes directly from the XML file. Once you have them as instances, you can manipulate them as you wish and rewrite them back into XML files just as easily.
In a performance critical application XmlReader/XmlWriter are a good choice (see here) for the sake of simplicity which is offered by Linq to XML and XmlDocument.
I've found the MvpXml project very useful in past scenarios where performance is a consideration. There's a wealth of knowledge about good practice within their project pages: http://www.codeplex.com/MVPXML
I'm considering Altova MapForce (or something similar) to produce either XSLT and/or a Java or C# class to do the translation. Today, we pull data right out of the database and manually build an XML string that we post to a webservice.
Should it be db -> (internal)XML -> XSLT -> (External)XML? What do you folks do out there in the wide world?
I would use one of the out-of-the-box XML serialization classes to do your internal XML generation, and then use XSLT to transform to the external XML. You might generate a schema as well to enforce that the translation code (whatever will drive your XSLT translation) continues to get the XML it is expecting for translation in case of changes to the object breaks things.
There are a number of XSLT editors on the market that will help you do the mappings, but I prefer to just use a regular XML editor.
ya, I think you're heading down the right path with MapForce. If you don't want to write code to preform the actual transformation, MapForce can do that for you also. THis may be better long term b/c it's less code to maintain.
Steer clear of more expensive options (e.g. BizTalk) unless you really need to B2B integration and orchestration.
What database are you using? Oracle has some nice XML mapping tools. There are some Java binding tools (one is http://java.sun.com/developer/technicalArticles/WebServices/jaxb). However, if you have the luxory consider using Ruby which has nice built-in "to_xml" methods.
Tip #1: Avoid all use of XSLT.
The tool support sucks. The resulting solution will be unmaintainable.
Tip #2: Eliminate all unnecessary steps.
Just translate your resultset (assuming you're using JDBC or equiv) to the outbound XML.
Tip #3: Assume all use of a schema-based tool to be incorrect and plan accordingly.
In other words, just fake it. If you have to squirt out some mutant SOAP (redundant, I know) payload just mock up a working SOAP message and then turn it into a template. Velocity doesn't suck.
That said, the best/correct answer, is to use an "XML Writer" style solution. There's a few.
The best is the one I wrote, LOX (Lightweight Objects for XML).
The public API uses a Builder design pattern. Due to some magic under the hood, it's impossible to create malformed XML.
Please note: If XML is the answer, you've asked the wrong question. Sometimes, we're forced against our will to use it in some way. When that happens, it's crucial to use tools which minimize developer effort and improve code maintainability.