Archive for category .NET

Modify CDATA Element in XML File using LINQ to XML

Yesterday I came across the following situation:

1) Load a XML File.

2) Parse the elements that match certain criteria.

3) Set the elements value to a new data

The only problem in the above situation was that I was dealing with CDATA inside the element.

Sample XML:

<employees>
    <employee>
        <description>
            <![CDATA[Lorem Ipsum]]>
        </description>
    </employee>
    ....
</employees>

The C# fragment that will allow you to manipulate the above elment is:

public void ParseEmployeeDescription(XDocument document)
{
	var result = from e in document.Descendants("description")
				 select e;
	foreach (var element in result)
	{
		if(/*Some condition matches*/)
		{
			var temp = element.Value;

			temp += "modified";
			//Replace with new data in the CDATA tag
			((XCData)element.FirstNode).Value = pTagData;

		}

	}
	document.Save("new_employees.xml");
}

Hopefully this helps someone :)

Post to Twitter

Tags: , , ,

ASP.NET MasterPage Caching Issues

A MasterPage is cached by the WebServer to give faster response to the user.

The caching occurs the first time it is the corresponding ContentPage is invoked.

Due to this fact the LoginView inside a Masterpage would be updated on some pages and would fail to update on others,
giving inconsistent results.

The solution to this problem is that you need to use control as seen below.

<div class="loginstatus">
      <asp:Substitution runat="server" ID="substituteLoginInfo" MethodName="methodToInvoke" />
</div>

The thing with control is that you need to point it to a static Method on the Masterpage Codebehind

It has the following signature :

public static string methodToInvoke(HttpContext context)
{
   return DateTime.Now.ToLongTimeString();
}

This method will return a HTML string that will be substituted in the page, hence the name of the control.

I read this on Scott Guthrie ‘s website and he referred to as “dough-nut cache”.

Where the cream part of the dough-nut is the substitution control which is not cached :)

Post to Twitter

Tags: , , ,