subject: Working On Xml In C# [print this page] If you're planning to build and alter xml files, or maybe you have to utilize XML and C#, you ought to look into .NET, an outstanding resource that provides terrific and extremely easy to use facilities for dealing with xml files.
The advantages making use of .NET and XML are additionally increased by using a set of Linq classes this means you could possibly use them in more general scenarios, which is ideal if you aren't that skilled in XML since it makes dealing with XML considerably more easier, particularly if you are a rookie.
What is particularly pleasing about Linq is that it uses a back to essentials approach relating to managing XML compared to other standard protocols like Xpath, DOM, SAX etc because these protocols are inclined to specialise or focus on just one particular component of XML, whilst Linq provides a more general and basic solution to working with XML.
By way of one example, lets examine how Linq handles a complicated data structure like a tree via XElement.
There's two core XML tags in XElement, and , so basically an opening and closing tag.
The principles are extremely basic and common with other markup languages like html, tags must be in equalled pairs and you can nest tags So for instance when you have tag opened with , you need a closing tag .
As a side note, you can also use an XML C# tool to generate C# code out of your xml or xml schema, that is certainly great for newcomers or if you wish to save time and have correct, error free code.
The only exemption to the matched pair guideline is if you utilize an own closing tag like this is a tag that ends itself.
So utilizing tags in XElement it's very easy and also straightforward to develop a tree like structure, you can even represent your tree in a program through a class.
The main point is always that XElement features a Nodes collection which is often useful to store an elements child elements.
The entire idea about nesting XElements within XElements is quite clear-cut however , you also can do this in another, more elaborate way, using a little something referred to as an Add method or using an Add method in a single call.
The below example should underscore this;
colour1.Add(colour2,colour3);
You can also create a list of child objects for placing into multiple XElement if you like or use an XElement constructor as another way to create your XML tree.
Another method is to utilize a nested tactic where you use an XElement with children and continue to the amount that you want, this is called a functional construction which, if you format it correctly, it should look just like the tree its is constructing and whats more, you may also pass it to virtually any method that would like to utilize it.
Last but not least, you can moreover make use of the Parse and static Load methods to transform your XML into an XElement tree.
The Load method works by taking your file specification as a URI (or as a TextReader or XmlReader) and parsing the actual text stream into your XElement tree.
The parse ,approach works much the same way other than it takes a string of XML tags. The principal point of note is that you may have to manage any resulting exceptions yourself if your XML is not correct, the parsing will not work.