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#)
Related
I'm looking to parse a xml file using Python and I was wondering if there was any way of automating the task over manually walking through all xml nodes/attributes using xml.dom.minidom library.
Essentially what would be sweet is if I could load a xml schema for the xml file I am reading then have that automatically generate some kind of data struct/set with all of the data within the xml.
In C# land this is possible via creating a strongly typed dataset class from a xml schema and then using this dataset to read the xml file in.
Is there any equivalent in Python?
lxml is a super-robust xml parsing package. It includes a subpackage, lxml.objectify, that will make an object tree from your xml.
It doesn't generate a class from the schema -- that's probably more a C#/Java thing -- but it does do schema validation so you know what kind of object you're getting back (see "asserting a schema").
You might take a look at lxml.objectify, particularly the E-factory. It's not really an equivalent to the ADO tools, but you may find it useful nonetheless.
hey dude - take beautifulSoup - it is a super library. HEAD over to the site scraperwiki.com
the can help you!
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.
What is the "best" way to search in xml?
Xpath or Linq2xml.
I'm asking this because we need to do a lot of searching in xml.
I'm always using XPath (since I've been using this from .NET 1.1). But with the introduction of Linq you can easely use Linq2Sql.
Regards,
M.
i use both extensively also xsl
they have very different uses imo
xpath is great for manipulating xml documents wheras linq2xml is great for mapping them into object collections.
In other words i regularly have applications that involve both.
for instance parsing csv into a given xml structure is almost cherry picked for xslt and xpath wheras linq2XMl will give you problems if you have an xml document that has optional elements. so i tend to use xpath to really lock down the xml format so that it is explicit and to keep my linq2xml mapping very very simple.
The result is a lot less bugs and much faster development.
no idea why the guy is talking about linq2xsd ... its a discontinued project that has very very little documentation. stay away from it.
Xdocument is an object that is actually enjoyable to work with ... xmldocument is one that is just fiddly imo. Obviously it depends on the task at hand, but the lack of xpath 2.0 makes me tend to use it as a data cleanser and then let linq2XMl do the real work.
as far as search goes, you can do everything that linq2xml does in xpath, the thing is that syntactically i far prefer to use linq2sql and play with strongly typed collections than mess about with xpath. Its much easier to come back to at a later date and adapt. Also you dont have to worry about syntax differences between xpath implementations and especially with regex implementations
Either. It depends.
Depending on your (and your team's) knowledge (i.e. XPath will not be effective is no one knows XPath, but all know LINQ to XML). Also some operations can be easier in one or the other.
You will need to define criteria first to judge what is best. And you need to decide whether you want to compare XPath 1.0 or 2.0 with LINQ to XML. Microsoft does not support XPath 2.0 but third party solutions exist, like Saxon 9 or like XQSharp.
Using JAXB in Java it is easy to generate from a xml schema file a set of Java classes that xml conforming to that schema can be deserialized to.
Is there some C# equivalent of JAXB? I know that Linq can serialize and deserialize classes to/from xml files. But how can I generate C# classes from xml schema file and then use this classes with linq?
If you're using Visual Studio, try the XML Schema Definition Tool. It takes your schema definitions and produces C# classes -- or it can go the other way and produce schema definitions from classes. It also has a number of other XML-related transformations.
There is a better tool from Microsoft called XsdObjectGen, the XSD Object Code Generator. It is like xsd.exe, but better. Also free, it is not part of the .NET SDK, but is a separate download.
Also see the SO question: XSDObjectGen vs Xsd.exe
Look into using DataSet. It's a bit of a different concept from using "Java Beans". The entire XML document is treated hierarchical set of tables all in a single class. The good part is that theory of encapsulation for OOP is actually enforced. Wow, Microsoft got something right that Sun pooched.
Anyway. You can also look at typed DataSet's if you want make things more interesting. I've used this on major projects with great success.
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