.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Working with XML and C#

Posted By:adrian shaw       Posted Date: May 25, 2014    Points: 200    Category: XML    URL: http://www.dotnetspark.com  

The advantages handling .NET and XML are further boosted with the addition of a set of Linq classes and so you might even rely on them in more general circumstances.

If you are intending to make and edit xml files, or maybe if you must work with XML and C#, you really should examine .NET, a very good aid which provides wonderful and incredibly simple to use facilities for working with xml files.

The advantages handling .NET and XML are further boosted with the addition of a set of Linq classes and so you might even rely on them in more general circumstances, which happens to be super if you aren't that expert in XML because it makes working with XML a great deal more less difficult, especially if you are a rookie.

What is especially appealing about Linq is that it uses a back to essentials approach pertaining to dealing with XML in comparison with other standard protocols like Xpath, DOM, SAX etc simply because these protocols usually tend to specialise or concentrate on just one distinct component of XML, whilst Linq provides a more general and basic solution to working with XML.

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

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

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

As a side note, you can also use an XML C# tool to create C# code out of your xml or xml schema, that is certainly ideal for rookies or if you want to save time and have correct, error free code.

The only exclusion to the matched pair principle is if you make use of an own closing tag like <colour/> which is a tag that closes itself.

So applying tags in XElement it is rather easy and even straightforward to create a tree like structure, you can also represent your tree in a program through a class.

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

The entire idea about nesting XElements within XElements is very easy but you might also repeat this in another, more elaborate way, making use of something known as an Add method or using an Add method in a single call.

The examples below example can show you this;


You may also create a list of child objects for entering into multiple XElement if you prefer or use an XElement constructor as yet another way to construct your XML tree.

Another method is using a nested tactic where you work with an XElement with children and continue to the levels that you need, this is known as a "functional construction" which, if you format it properly, it should appear the same as the tree its is constructing and what's more, it is also possible to pass it to just about any method that needs to use it.

Lastly, you can also make use of the Parse and static Load methods to convert 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 major 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 number of choices for producing or modifying XML files in C#, as well as manual coding.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend