Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Handling XML in C#

2 views
Skip to first unread message

sarah wilkes

unread,
May 13, 2014, 2:52:22 PM5/13/14
to
If you are intending to make and alter xml files, or maybe you must deal with XML and C#, you might like to take a look at .NET, a fantastic resource which provides excellent and incredibly easy to use amenities for dealing with xml files.

The benefits of making use of .NET and XML are even more improved upon by using a set of Linq classes therefore you may possibly utilize them in more general conditions, which is ideal if you are not that skilled in XML as it makes working with XML a great deal more less difficult, particularly if you are a novice.

What is especially pleasing about Linq is that it takes a back to essentials approach in terms of working with XML in comparison to other standard protocols like Xpath, DOM, SAX etc because these protocols tend to specialise or concentrate on just one particular area of XML, as opposed to Linq offers a more general and simple approach to working with XML.

By means of one good example, let's check out how Linq handles a complicated data composition like a tree through XElement.

There are 2 core XML tags in XElement, <record> and </record>, so basically an opening and closing tag.

The guidelines are extremely easy and common with other markup languages like html, tags have to be in coordinated sets and you may nest tags So for instance when you have tag opened with <colour>, you need a closing tag </colour>.

As a side note, you may also use an http://www.liquid-technologies.com/XmlDataBinding/Xml-Schema-To-Cs.aspx to create C# code out of your xml or xml schema, which happens to be ideal for novices or maybe you wish to save time and have correct, error free code.

The only exemption to the matched pair guideline is if you employ an own closing tag like <colour/> this is a tag that closes itself.

So utilizing tags in XElement it is quite easy as well as simple to build a tree like structure, it's also possible to represent your tree in a program through a class.

The main point is the fact that XElement features a Nodes collection which may be employed to store an element's child elements.

The whole idea about nesting XElements within XElements is very simple but you might also repeat this in another, more elaborate means, applying something known as an Add method or applying an Add method in a single call.

The following example can demonstrate this;

colour1.Add(colour2,colour3);

It's also possible to construct a list of child objects for putting into multiple XElement if you prefer or use an XElement constructor as one more way to construct your XML tree.

Another way is to apply a nested technique in which you work with an XElement with children and continue to the levels that you want, this is what's called a "functional construction" which, if you format it the right way, it's going to appear just like the tree its is constructing and what's more, it's also possible to pass it to almost any method that would like to utilize it.

Lastly, you can additionally make use of the Parse and static Load methods to transform your XML into an XElement tree.

The Load method operates by taking your file specification as a URI (or as a TextReader or XmlReader) and parsing the text stream into your XElement tree.

The parse ,technique works much the same way apart from it takes a string of XML tags. The main point of note is that you might have to manage any resulting exceptions yourself if your XML is not right, the parsing will not work.

To conclude, there are a variety of options for developing or modifying XML files in C#, together with manual coding, or you could employ an XML C# tool such as http://www.liquid-technologies.com/XmlDataBinding/Xml-Schema-To-Cs.aspx, which in turn saves you time in creating thousands of lines of error free code, instantly from your xml or schema file.





0 new messages