Monday, June 21, 2010

XQuery - Projecting Attributes With a Where Clause a Query

Had to write an xquery yesterday to extract some attribute values from an xml column, so I thought of posting it here for future reference.

This the structure of my XML data stored in the column xmlColumn

This is the records that I have in my table

What I need is to select the Id column from a normal table column and then the "value" attributes from the xml coulumn whose "SiteText" is equal to 1

This is the query written in 2 different ways

select id, xmlColumn.
from temp

select id, xmlColumn.
value('data(AuditRecord/Records/Record[@SiteText=1]/@value)[1]', 'varchar(50)')
from temp

and this is the result set I get

Tuesday, June 15, 2010

Validating XML with an XSD using LINQ

So how do you validate an XML document that you get with a particular XSD?.
One way you can do is with LINQ to XML with these steps.
1) Create a XmlSchemaSet
2) Add the schemas that your document needs to confirm to
3) Call the validate method of the XDocument

So here is the example code.

//Load an xml document
XDocument doc = XDocument.Load(@"C:\test.xml");

//Define the schema set
XmlSchemaSet set = new XmlSchemaSet();

//Add the schema to the set
set.Add(string.Empty, @"C:\test.xsd");

//A variable to test whether our document is valid
bool isValid = true;

//Cal the validate method
//Note that I am using a lambda function , alternativley you can use
//pass in a delegate method as in the traditional method :)
doc.Validate(set, (o, e) => { isValid = false; });

//Print out the result

That's it !!.

So what if you want a WCF service to validate this? can accept a stream or a string as the input, create the XDocument and then validate it.

Another way you can enforce the validation is generate a class hierarchy that confirms to the XSD and expose that has the parameter of the WCF method.
In this case the client has no other choice but to use the class types, and if the input you get to the WCF service does not serialize correctly to the XML confirming to the XSD an error will be thrown.

Exposing Enum Types From WCF

There are many cases where you would want to expose enum types from a WCF service, so how do you do this?

Simply create a data contract and mark the values with the EnumMember attribute and thats it

public enum MyEnumName
XML = 2

Monday, June 14, 2010

The maximum nametable character count quota (16384) has been exceeded

Some of our services were growing and the other day it hit the quote, I could not update the service references, nor was I able to run the WCFTest client. An error is diplayed saying "The maximum nametable character count quota (16384) has been exceeded "
The problem was with the mex endpoint, where the XML that was sent was too much for the client to handle, this can be fixed by do the following.
Just paste the lines below within the configuration section of the devenve.exe.config and the svcutil.exe.config files found at the locations

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE,
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

Restart IIS and you are done.
The detailed error that you get is the following :

Error: Cannot obtain Metadata from net.tcp://localhost:8731/Services/SecurityManager/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at Exchange Error URI: net.tcp://localhost:8731/Services/SecurityManager/mex Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8731/Services/SecurityManager/mex'. There is an error in the XML document. The maximum nametable character count quota (16384) has been exceeded while reading XML data. The nametable is a data structure used to store strings encountered during XML processing - long XML documents with non-repeating element names, attribute names and attribute values may trigger this quota. This quota may be increased by changing the MaxNameTableCharCount property on the XmlDictionaryReaderQuotas object used when creating the XML reader.