Modify child nodes of a rdlc report which is loaded into XML Document

Dear friends,

The following code snippet will help you to modify the child nodes of an XML document quickly. You just need to call this function in the right manner and the XML will be modified. I have loaded a RDLC report into XML document and then modify its child nodes dynamically. A RDLC report contains nodes like TextBox, Image, Rectangle. Now suppose you want to modify the top property of a TextBox at runtime. The code below will help you to do so. For the function below, you need to call it in a manner like this:

ModifyChildNodeInsideTheParentTag(objXmlDocument, TextBox, txtSample, Top, "5in");

This will search in all textboxes in the report(xml doc) for txtSample and then set the Top property as 5 inches.

This code is very easy to understand and very useful if you need to change the values in a rdlc again and again.


/// summary
/// Method to modify tags like the width property of a textbox called 'txtSample' in RDLC
/// param name="objXmlDocument":-xml document object which is actually the rdlc report
/// param name="strUniquelyIdentifiedParentControlName":-name of parent tag whose child is to be modified like TextBox, Image, Rectangle
/// param name="strUniqueIdentifierOfParentControl":-unique identifier such as 'name' attribute of the parent tag
/// param name="strTagNameWhichIsToBeEdited" :- actual node which is to be modified like the width, left, top
/// param name="strNewValue":-new value
/// returns modified xml document object
public XmlDocument ModifyChildNodeInsideTheParentTag(XmlDocument objXmlDocument, string strUniquelyIdentifiedParentControlName, string strUniqueIdentifierOfParentControl, string strTagNameWhichIsToBeEdited, string strNewValue)
{
try
{
XmlNamespaceManager objXmlNamespaceManager = new XmlNamespaceManager(objXmlDocument.NameTable);
objXmlNamespaceManager.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
objXmlNamespaceManager.AddNamespace("reportDefinition", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");
//strUniquelyIdentifiedParentTagName is the control name like TextBox, Image etc.
XmlNodeList nodeList = objXmlDocument.SelectNodes("//reportDefinition:" + strUniquelyIdentifiedParentControlName, objXmlNamespaceManager);
// Nodelist will contain all Textboxes of the report.
if (nodeList != null)
{
foreach (XmlNode node in nodeList)
{
if (node.Attributes.Count > 0)//main tag
{
// In the textboxes list, we need to look for a particular textbox e.g. 'txtSample'
if (node.Attributes[0].Value == strUniqueIdentifierOfParentControl)
{
int nChildNodeCount = node.ChildNodes.Count;
//Loop through all the childNodes of that textbox
for (int i = 0; i < nChildNodeCount; i++)
{
//ActualTagName is the name of the tag which we want to edit like width, top etc.
if (node.ChildNodes[i].Name == strTagNameWhichIsToBeEdited)
{
//If the 'ActualTagName' is found, set the new value
node.ChildNodes[i].InnerText = strNewValue;
break;
}
if (node.ChildNodes[i].HasChildNodes)
{
int nCountChildNodes = node.ChildNodes[i].ChildNodes.Count;
for (int j = 0; j < nCountChildNodes; j++)
{
if (node.ChildNodes[i].ChildNodes[j].Name == strTagNameWhichIsToBeEdited)
{
node.ChildNodes[i].ChildNodes[j].InnerText = strNewValue;
break;
}
}
}
}
}
}
else
{
if (node.Name == strTagNameWhichIsToBeEdited)
{
node.InnerText = strNewValue;
}
}
}
}
}
catch (Exception ex)
{
Exception m_eException = new Exception("Exception - > ", ex);
throw m_eException;
}
return objXmlDocument;
}


To know more on how to load the RDLC report into an XML document, please refer the code snippet named 'How to load a rdlc file from resources folder into an XML document dynamically?'

Cheers!


Comments

Author: Md. Shohel Rana30 Apr 2013 Member Level: Bronze   Points : 0

Greate Post,Manay Many Thanks

Guest Author: Peggy27 May 2013

That's way more clever than I was expecting. Thanks!



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: