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

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());

}

}

Saturday, August 6, 2011

Special Characters and XML Strings

Special Characters and XML Strings
XML has a special set of characters that cannot be used in normal XML strings. These characters are:



& - &amp;
< - &lt;
> - &gt;
" - &quot;
' - &apos;
For example, the following XML string is invalid:


<Organization>Santa & Banta</Organization>Whereas the following is valid XML:

<Organization>Santa &amp; Banta</Organization>—Note that we have replaced '&' with '&amp;' in the second XML string which makes it valid.

Monday, July 11, 2011

Read from XML file using c#

following sample uses XmlTextReader to read from XML file

XmlTextReader reader = new XmlTextReader(MapPath("~/XML/States.xml"));
while (reader.Read())
{ switch (reader.NodeType)
{

case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
while (reader.MoveToNextAttribute()) // Read the attributes.
Console.Write(" " + reader.Name + "='" + reader.Value + "'");
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType. EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
 
//sample XML file
<?xml version="1.0" encoding="utf-8" ?>

<Items<Item ddlValue ="1" ddlText = "Yes" />
<Item ddlValue ="2" ddlText = "No" />
</Items>

Create XML string using XmlTextWriter class

The Basics of the XmlTextWriter Class
The XmlTextWriter class contains a number of methods that are useful for starting and completing an XML document and for adding elements and attributes to the XML document. The most important methods are:

WriteStartDocument() - you should call this method to start creating an XML document. This will create the first line in the XML document, specifying that the file is an XML document and its encoding.


WriteStartElement(string) - this method creates a new element in the XML document with the name specified by the string input parameter. (You can also specify a namespace as a second, optional string parameter.)


WriteElementString(name, text_value) - If you want to create an XML element with nothing but text content (i.e., no nested elements), you can use this method.

WriteAttributeString(name, value) - this method writes an attribute name and value to the current element.


WriteEndElement() - this method closes off the element created in the WriteStartElement(string) method call.

WriteEndDocument() - this method completes the writing of the XML document.

Example:

<root>
<Employee>
<EmployeeID>
6
</EmployeeID>
<Name>
Pa Ji
</Name>


</Employee>
</root>

private string GenerateXmlString()
{
StringBuilder xmlBuilder = new StringBuilder( 1000 );
TextWriter xmlWriter = new StringWriter( xmlBuilder );
XmlTextWriter xmlMessage = new XmlTextWriter( xmlWriter );
xmlMessage.WriteStartElement( "root" );
xmlMessage.WriteStartElement( "Employee" );
xmlMessage.WriteStartElement( "EmployeeID" );

xmlMessage.WriteString("6");

xmlMessage.WriteEndElement( );
xmlMessage.WriteStartElement( "Name" );
xmlMessage.WriteString( "Pa Ji");

xmlMessage.WriteEndElement() ;

xmlMessage.WriteEndElement( ); // </Employee>

xmlMessage.WriteEndElement( ); // </Root>
return xmlBuilder.ToString( );
}

Parse XML string sample

There are five main techniques to parse xml files. Each technique is based on a different .NET Framework class and its associated methods:

  • XmlTextReader
  • XmlDocument
  • XPathDocument
  • XmlSerializer
  • DataSet

In the following sample I will explain how to perform this using xmlDocument


string sXML = "" +
"<LostFound MyFormID= \"12 \"> " +
"<FormID>12</FormID>" +
" <LostFoundLogic> " +
" <TargetTypeID>1</TargetTypeID> " +
" <TargetID>2</TargetID> " +

" </LostFoundLogic> " +
" " +
" <LostFoundLogic> " +
" <TargetTypeID>111</TargetTypeID> " +
" <TargetID>222</TargetID> " +

" </LostFoundLogic> " +

"</LostFound> ";


XmlNode objNode = null;
string strXml = sXML;
if (strXml.Length > 0)
{
XmlDocument select = new XmlDocument();
select.LoadXml(strXml);
XmlNodeList LostFoundList = select.GetElementsByTagName("LostFoundLogic");


foreach (XmlNode anode in LostFoundList)

{
objNode = anode.SelectSingleNode("TargetTypeID"); if (objNode != null)
{

int TargetTypeID = Convert.ToInt32(anode.SelectSingleNode("TargetTypeID").InnerText);

}
int TargetID = Convert.ToInt32(anode.SelectSingleNode("TargetID").InnerText);
}

XmlNode node;

node = select.SelectSingleNode("/LostFound/FormID");

string FormID = node.InnerText;



//Retreieve MyFormID attribute
XmlNode dsEventType = xmlDiary.GetElementsByTagName("LostFound")[0];
sEventType = dsEventType.Attributes[ MyFormID"].Value + "";

}

How to upload app to macOS

1. Open Terminal Press Cmd (⌘) + Space , type Terminal , and hit Enter . 2. Navigate to Your Build Output Directory Your .app file is likel...