
If you are
intending to build and alter xml files, or if perhaps you might want to deal
with XML and C#, you might like to look into .NET, an excellent tool that
provides superb and really simple to use amenities for working with xml files.
The advantages
utilizing .NET and XML are further improved by using a set of Linq classes and
so you may possibly make use of them in more general circumstances, which is
great if you are not that expert in XML as it makes handling XML much more
simpler, especially if you are a newbie.
What's specially
interesting about Linq is that it uses a back to basics approach regarding
managing XML compared to other standard protocols like Xpath, DOM, SAX etc
because these protocols tend to specialise or concentrate on just one specific
part of XML, as opposed to Linq provides a more general and basic way to
working with XML.
Through an
illustration, let's have a look at how Linq addresses a complicated data
composition like a tree through XElement.
There are two
core XML tags in XElement, and , basically an opening and closing tag.
The principles
are extremely clear-cut and common with other markup languages like html, tags
have to be in corresponding pairs and you can nest tags So one example is if
you have tag opened with , you need a closing tag .
As a side note,
you can even use an XML C# tool to create C# code out of your xml or xml
schema, that is great for novices or if you want to save time and have correct,
error free code.
The only
exception to the matched pair guideline is if you use an own closing tag like
this is a tag that ends itself.
So working with
tags in XElement it is quite easy and even simple to create a tree like
structure, you may also represent your tree in a program through a class.
The main point
is always that XElement encompasses a Nodes collection which can be utilized to
store an element's child elements.
The whole idea
about nesting XElements within XElements is rather simple but you also can
repeat this in another, more elaborate means, utilizing anything called an Add
method or applying an Add method in a single call.
The following
example can demonstrate this;
(colour2,colour3);
You may also
construct a list of child objects for inserting into multiple XElement if you
choose or use an XElement constructor as another way to create your XML tree.
Another method
is using a nested method where you use an XElement with children and continue
to the level that you want, this is what's called a ""functional construction""
which, if you format it properly, it should appear just like the tree its is
constructing and what's more, you may as well pass it to just about any method
that needs to work with it.
Finally, you can
moreover make use of the Parse and static Load methods to convert your XML into
an XElement tree.
The Load method
functions 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 will work much the same way apart from it will take a string of XML
tags. The major point of note is that you could have to handle any resulting
exceptions yourself if your XML is not right, the parsing will not work.

0 Response to "Working On XML in C Sharp"
Post a Comment