Wriju's BLOG

.NET, Cloud and everything

LINQ to XML : Handling blank or no Element and Attribute

It quite so happen that you are working with XML where you are expecting a specific element in every set. But somehow that is missing in some of the sets. Now at runtime you would get an error.. The XML file which I am targeting <?xml version="1.0" encoding="utf-8"?> <Employees> <Employee Location="Earth"> <Name>Wriju</Name> <Email>a@a.com</Email> </Employee> <Employee… Read more

LINQ to XML : Understanding Annotations

Annotations are used for private use. It does not directly associated and known as Black Box in XML world. This can also be considered as meta tag. Below is an example on how it can be handled. I have created a sample Windows Applications and will try to capture my understanding. /// <summary> /// Class… Read more

LINQ – Uniformity All Over

I have spent a while with LINQ and still feel very new whenever I explore some new power. Here I am going to describe you how LINQ uniformly allows you write for the various types of data.   In-memory data source ++++++++++++++++ List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7,… Read more

LINQ to XML : Querying XML with Namespaces

This question arises in one of discussions. If you have a XML document with “xmlns” then LINQ to XML does not return any IEnumerable<T> J. This is strange but true for XPath and XQuery. There is a trick to get it done, Let us assume that you have a XML as follows, <?xml version=“1.0“ encoding=“utf-8“?>… Read more

LINQ : IEnumerable<T> and IQueryable<T>

IEnumerable<T> and IQueryable<T> are the two most used terms of any LINQ discussion. What I am trying to here is that I am trying to simplify the two interfaces depending on their behavior. In LINQ world we generally have few providers available within .NET Framework, like LINQ to Object, LINQ to SQL, LINQ to XML…. Read more

LINQ to XML : Modifying XML document

You have XML document now you want to modify that XML file using LINQ to XML. It is as easy as you generally modify any database column value.   Let us create a dummy XML stream,   //Create dummy XML to work var root = new XElement(“parent”,     from i in new int[] { 1,… Read more

LINQ to XML : Changing connectionString in app.config

When you create data bind application using wizard in Windows Forms application and connection string gets added to you settings file. Now you may be interested in changing that connection string but problems, 1)     The connection string in settings has an Application Scope so it is ReadOnly property. You modify and remove “ReadOnly” from .vb… Read more

LINQ to XML : Adding Namespace quickly

I have written a post earlier on how to attach Namespaces. After reading my article someone complained about the redundancy of the code. I also realized the pain. I was reading the book C# 3.0 In a Nutshell. There I got a very elegant solution. Let me explain the scenario,   If you create a… Read more

LINQ to XML : Join Xml Data

Let’s say I have created two Xml files using LINQ to XML from Northwind database. I have taken two tables Category and Products and tried to join between two different files.   Category XML   <?xml version=“1.0“ encoding=“utf-8“?> <categories>   <category id=“1“>     <CategoryName>Beverages</CategoryName>   </category>   <category id=“2“>     <CategoryName>Condiments</CategoryName>   </category>   <category… Read more

LINQ to XML : Working with Prefixes

Based on my previous post on LINQ to XML : Working with Namespaces, if you want to add prefixes to your Xml things becomes little crazy. The trick is that you have to use XAttribute() to attach prefixes. So the Xml as below need the following code,     <pfx:root xmlns:pfx=“urn:mynamespace-com“>   <pfx:child /> </pfx:root>… Read more

LINQ to XML : Working with Namespaces

If you are working with complex real world XML then you have to deal with namespaces to disambiguate. There are several ways to implement namespaces in LINQ to XML. One is you can use pure string to add your namespace with each element declaration, like   <root>   <child /> </root>   To create the… Read more

LINQ to XML : Creating complete XML document

LINQ to XML API allows us to create complete XML document as expected with all the elements. So this X-DOM has everything as you expect.   Simple sample looks like,     XDocument doc = new XDocument(     new XDeclaration(“1.0”, “utf-16”, “true”),     new XProcessingInstruction(“test”, “value”),     new XComment(“This is comment by you”),     new… Read more

LINQ to XML : Creating XML from another XML

Based on my previous post if I have to filter the list of customers only in the country “US” and create another XML which may look like,   <?xml version=“1.0“ encoding=“utf-8“?> <usaCustomers>   <usaCustomer>     <title>Marketing Manager</title>     <name>Howard Snyder</name>   </usaCustomer>   <usaCustomer>     <title>Sales Representative</title>     <name>Yoshi Latimer</name>   </usaCustomer>   <usaCustomer>    … Read more

LINQ to XML : Creating complex XML through LINQ

We can generate hierarchical object graph in our memory though LINQ. To be more realistic we can bring data from relational database. So if we consider Northwind database and use LINQ to SQL to bring all the Customers and their Orders and Order Details the query would look like,     //LINQ to SQL way… Read more

LINQ to XML : Two important classes XElement and XAttribute

XElement and XAttribute are the two  very important classes available in System.Xml.Linq.dll assembly. Using these two classes you can do lot of things in the LINQ to XML world. I will show you step by step how,   For just an element   XElement _root = new XElement(“root”);   _root.Save(fileName);   And the output will… Read more