Wednesday, March 7, 2012

Updating XML data and binding it to GridView

Following sample shows how data is updated in XML and then XML string is binded to GridView
private void SampleXmlDataUpdateAndBind()

{

try

{



bool bAddNewNode = false;

string sXML = "";



XmlDocument objDocXML = new XmlDocument();

XmlNodeList xnList = null;

XmlNode objNode = null;

int iResourceCount = 0;

string sResourceName = "";

foreach (clsCallSchedular u in mObjSchedular)

{

bAddNewNode =
false;

if (String.IsNullOrEmpty(sXML))

{

bAddNewNode =
true;

}

else

{

objDocXML.LoadXml(
"<xml>" + sXML + "</xml>");

xnList = null;

xnList = objDocXML.SelectNodes("/xml/ResourceShiftInfo[@ResourceID=\"" + u.ResourceID + "\" and @ShiftID = \"" + u.EventTypeID + "\"]");

if (xnList.Count == 0)

{

bAddNewNode =
true;

}

else

{

objNode =
null;

foreach (XmlNode anode in xnList)

{

objNode = anode.SelectSingleNode(
"ResourceCount");

if (objNode != null)

{

iResourceCount =0;

if (anode.SelectSingleNode("ResourceCount").InnerText != null)

iResourceCount = Convert.ToInt32(anode.SelectSingleNode("ResourceCount").InnerText);

anode.SelectSingleNode("ResourceCount").InnerText = (iResourceCount + 1).ToString();

sXML = objDocXML.OuterXml;

sXML = sXML.Replace(
"<xml>","");

sXML = sXML.Replace("</xml>", "");

}

}

}

}

 

if (bAddNewNode)

{

sResourceName = u.ResourceName +
"";

if (String.IsNullOrEmpty(sResourceName))

{

sResourceName =
"Resource not Assigned";

}

sXML = sXML +

"<ResourceShiftInfo ResourceID= \"" + u.ResourceID + "\" ShiftID =\"" + u.EventTypeID + "\">" +

"<ResourceName>" + sResourceName + "</ResourceName>" +

"<Shift>" + u.EventType + "</Shift>" +

"<ResourceCount>" + 1 + "</ResourceCount>" +

"</ResourceShiftInfo>";

}

}

if (!String.IsNullOrEmpty(sXML))

{

sXML = sXML.Replace(
"<xml>", "");

sXML = sXML.Replace("</xml>", "");

sXML = "<xml>" + sXML + "</xml>";

 

DataSet aDataSet = new DataSet();

aDataSet.ReadXml(new StringReader(objDocXML.OuterXml));

grdViewResourceShiftCount.DataSource = aDataSet;

grdViewResourceShiftCount.DataBind();

}

 

}

catch (Exception ex)

{

Response.Write(ex.ToString());

}

}

No comments:

Post a Comment

Open default email app in .NET MAUI

Sample Code:  if (Email.Default.IsComposeSupported) {     string subject = "Hello!";     string body = "Excellent!";    ...