Search

Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Sep 24, 2008

with operator in JavaScript

Hello Friends,

I found one good thing in JavaScript, there is with operator in JavaScript, which you can say same as with operator in VB.NET. Its always helpful to define short name of big variable or replacement of such big variable; which lead us to make code more readable and maintain too.

In general when we are dealing with XML datasrouce in JavaScript; there always have long long variable name, of the value is so deep inside.

xmlDoc.childNodes[1].childNodes[0].childNodes[3].childNodes[0].text
xmlDoc.childNodes[1].childNodes[0].childNodes[4].childNodes[0].text

We have to write more lines of code to get all the details. Here we can use with, as we know part of path to reach final value is common which is xmlDoc.childNodes[1].childNodes[0], so we can put it into with keyword.

with(xmlDoc.childNodes[i].childNodes[0])
{
name = childNodes[1].childNodes[0].text;
add1 = childNodes[2].childNodes[0].text;
add2 = childNodes[3].childNodes[0].text;
email = childNodes[4].childNodes[0].text;
.
.
.
}

Aug 19, 2008

Best way to generate XML using LINQ

Hi all,

As we know C# 3.0 with LINQ gives us lots of power and smart coding, here is one more example which uses features of LINQ to generating XML of the abstract type.

I will give you example where you need to display the person details along with the address, here address can be anything like home address, email address, IM address ...

Here are the classes which require to achive our goal.

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Address> Address = new List<Address>();
}

class Address
{
public string AddressValue { get; set; }
public string Type { get; set; }
}

Person class used to store person data, and Address class which holds the type of address.
Now lets add few details.

Person Person1 = new Person() { FirstName = "Imran", LastName = "Bhadelia" };
Person1.Address.Add(new Address() { Value = "imran.bhadelia[at]gmail.com", Type = "Email" });
Person1.Address.Add(new Address() { Value = "bhadelia.imran[at]gmail.com", Type = "AlternativeEmail" });
Person1.Address.Add(new Address() { Value = "bhadelia.imran[at]gmail.com", Type = "MSNInstanceMessanger" });
Person1.Address.Add(new Address() { Value = "bhadelia.imran", Type = "YahooInstanceMessanger" });

Person Person2 = new Person() { FirstName = "Armaan", LastName = "Bhadelia" };
Person2.Address.Add(new Address() { Value = "armaan.bhadelia[at]gmail.com", Type = "Email" });
Person2.Address.Add(new Address() { Value = "Sweet Home, 406 Paradise Palace...", Type = "Residence" });
Person2.Address.Add(new Address() { Value = "bhadelia.armaan[at]gmail.com", Type = "MSNInstanceMessanger" });
Person2.Address.Add(new Address() { Value = "bhadelia.armaan", Type = "YahooInstanceMessanger" });

List<Person> PersonList = new List<Person>();
PersonList.Add(Person1);
PersonList.Add(Person2);

Here I used one feature of C#3.0, which is Object Initialization Expressions. See the constructor of both class, Its default and while creating object I am setting the value to the fields of class [Smart coding]. I added this two class into List variable, from where LINQ will generate XML which looks like...

<Persons>
<Person FirstName="Imran" LastName="Bhadelia">
<Address Email="imran.bhadelia[at]gmail.com" />
<Address AlternativeEmail="bhadelia.imran[at]gmail.com" />
<Address MSNInstanceMessanger="bhadelia.imran[at]gmail.com" />
<Address YahooInstanceMessanger="bhadelia.imran" />
</Person>
<Person FirstName="Armaan" LastName="Bhadelia">
<Address Email="armaan.bhadelia[at]gmail.com" />
<Address Residence="Sweet Home, 406 Paradise Palace..." />
<Address MSNInstanceMessanger="bhadelia.armaan[at]gmail.com" />
<Address YahooInstanceMessanger="bhadelia.armaan" />
</Person>
</Persons>

And now LINQ to generate above XML.

var PersonXml =
new XElement("Persons",
from person in PersonList
select (new XElement("Person",
new XAttribute("FirstName", person.FirstName),
new XAttribute("LastName", person.LastName),

from addr in person.Address
select new XElement("Address", new XAttribute(addr.Type, addr.Value)))));

Console.WriteLine(PersonXml.ToString());

How it works??
Line#2: Create the main element which is Persons
Line#3: Getting single Persons object from PersonList
Line#4: Creating sub element to Persons which is Person
Line#5&6: Creating attributes to show person details.
Line#8: Getting Address of the person from Address collection
Line#9: Creating Address element along with the attribute

Easy? I found its very easy :)

Now if you want to add this information into SQL Server 2005 and higher, you can pass it as string and in your procedure grab the value. There are lots of way to grab value from XML in SQL, you can check this url, it has almost all the operations to XML datatype in SQL.

I created SQL script for specific to this xml.

SELECT 
Person.value('@FirstName[1]', 'VARCHAR(100)') as FirstName,
Person.value('@LastName[1]', 'VARCHAR(100)') as LastName,
Person.value('(Address/@Email)[1]', 'VARCHAR(100)') as Email,
Person.value('(Address/@AlternativeEmail)[1]', 'VARCHAR(100)') as AlternativeEmail,
Person.value('(Address/@Residence)[1]', 'VARCHAR(100)') as Residence,
Person.value('(Address/@MSNInstanceMessanger)[1]', 'VARCHAR(100)') as MSNInstanceMessanger,
Person.value('(Address/@YahooInstanceMessanger)[1]', 'VARCHAR(100)') as YahooInstanceMessanger
FROM @Persons.nodes('/Persons/Person') p(Person)

Jul 31, 2008

Limitations of the XML Data Type

Hi all,

I found the limitation of XML Data Type introduced in SQL Server 2005.

Although the XML datatype is treated like many other datatypes in SQL Server 2005, there are specific limitations to how it is used. These limitations are:


  1.    XML types cannot convert to text or ntext data types.

  2.    No data type other than one of the string types can be cast to XML.

  3.    XML columns cannot be used in GROUP BY statements.

  4.    Distributed partitioned views or materialized views cannot contain XML data types.

  5.    Use of the sql_variant instances cannot include XML as a subtype.

  6.    XML columns cannot be part of a primary or foreign key.

  7.    XML columns cannot be designated as unique.

  8.    Collation (COLLATE clause) cannot be used on XML columns.

  9.    XML columns cannot participate in rules.

  10.    The only built-in scalar functions that apply to XML columns are ISNULL and COALESCE. No other scalar built-in functions are supported for use against XML types.

  11.    Tables can have only 32 XML columns.

  12.    Tables with XML columns cannot have a primary key with more than 15 columns.

  13.    Tables with XML columns cannot have a timestamp data type as part of their primary key.

  14.    Only 128 levels of hierarchy are supported within XML stored in the database.

Jan 21, 2008

Passing lists to SQL Server 2005 with XML Parameters

We frequently have requirement like

Passing IDs in comma saperated value to the sql server for further processing.

Generally what we do is just create loop and get the value from it and ...

But SQL 2005 comes up with XQuery. Using this you can easly pass such type of data also some complex thing too.

For example...

DECLARE @productIds xml
SET @productIds ='<Products><id>3</id><id>6</id><id>15</id></Products>'


SELECT ParamValues.ID.value('.','VARCHAR(20)')FROM @productIds.nodes('/Products/id') as ParamValues(ID)

Which gives us the following three rows:

3
6
15

Read more..

Jun 14, 2007

Create .NET documentation with Microsoft's Sandcastle

Hello all,

This is post for developers who are using xml comments.

The .NET Framework allowed C# developers to use XML-style comments in their code. This feature was added to VB.NET with version 2.0. The compiler can use these comments to generate basic technical documentation. The end result of using the XML commenting feature is a large XML file that is less than user friendly

How to get Sandcastle



The latest version of Sandcastle is available via href="http://www.microsoft.com/downloads/details.aspx?FamilyId=E82EA71D-DA89-42EE-A715-696E3A4873B2&displaylang=en">Microsoft's
March 2007 Community Technology Preview
. You may install and run it on
Windows Server 2003 or Windows XP Service Pack 2. It requires the href="http://msdn2.microsoft.com/en-us/netframework/aa731542.aspx
">.NET
Framework 2.0
, as well as the href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/htmlhelp/html/hwMicrosoftHTMLHelpDownloads.asp">HTML
Help Workshop
. Once the prerequisite software is installed, you can install
the Sandcastle tool.



I also have delicious Create .NET documentation with Microsoft's Sandcastle .