LINQ to XML Part #4: Different way of Parsing string to create XML tree

No.of Views823
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Apr 2010 11:04:01
Tag : LINQ , Miscellaneous
I will show different way of parsing string to create XML tree using LINQ to XML.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I will show different way of parsing string to create XML tree using LINQ to XML.

What is Parsing of XML document?

Parsing of XML document means reading XML document, identifies the function of each of the document and then makes this information available in memory for rest of the program.

XElement.Parse () method

1.    This method is used to parse a string.
2.    This is an overloaded method.

Methods are as below.
 

Image Loading

 

Image Loading

2nd overloaded method is having a parameter LoadOptions; this parameter defines whether to preserve space line information or not.

LoadOptions enum

1.    This is inside System.Linq namespace.
2.    This enum is having 4 properties.
 

using System;

namespace System.Xml.Linq
{
    
    [Flags]
    public enum LoadOptions
    {
        None = 0,        
        PreserveWhitespace = 1,
        SetBaseUri = 2,        
        SetLineInfo = 4,
    }
}

Way # 1 Parsing String to create XML Tree

In this sample, I will create a XML tree from string.
1.    Using first method to create XML Tree.
2.    There is only one parameter being passed.

XElement xmltree = XElement.Parse(@"<Address><Name>Dhananjay Kumar </Name> <Road> Padma Road </Road> </Address>");
Console.WriteLine(xmltree);

Sample Output

Image Loading

In this sample, I will create a XML tree from string.
1.    Using second method to create XML Tree.
2.    There is two parameter being passed.
3.    We are passing preserve space as load options.
 

XElement xmltree = XElement.Parse(@"<Address><Name>Dhananjay Kumar </Name> <Road> Padma Road </Road> </Address>",LoadOptions.PreserveWhitespace);
Console.WriteLine(xmltree);

Sample Output

We can see the difference in output.  That white space is preserved.

 

Image Loading

Conclusion

In this article, I explained how to parse a string to create XML tree.  Thanks for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    I was working on the project using LINQ. I got the requirement to join the two entity on multiple column.
    Published Date : 02/Oct/2011
    I have found three more new operators in Linq which is use full in day to day programming stuff. Take,Skip and Reverse. Here are explanation of operators how it works.
    Published Date : 21/Jun/2010
    Projection helps us developer to retrieve desired result from the collection . LINQ provides two projection operator. Select and SelectMany. Select works with one collection whereas SelectMany works with more than one collection
    Published Date : 20/Aug/2010
    In this article, I will give explanation on XElement class. This class is used to construct XML Elements.
    Published Date : 17/Apr/2010
    In this article, I will give explanation on XAttribute class. This class is used to construct Attributes in XML Elements.
    Published Date : 17/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top