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

Xml operation

****************Creating new xml file************************

XmlTextWriter newXmlFile = new   XmlTextWriter(Give path and name of xml file to create,System.Text.Encoding.Default);

newXmlFile.WriteStartDocument();

//creating the name of the main node
newXmlFile.WriteStartElement("Start","");

//creating child nodes
newXmlFile.WriteElementString("First");
newXmlFile.WriteFullEndElement();
       newXmlFile.WriteEndElement();
newXmlFile.Flush();
newXmlFile.Close();

************************Insert data to xml file************************

// Create xml dom
XmlDocument XMLDom = new XmlDocument();

//load your xml file
XMLDom.Load(@"path of your xml file");

//Select main node
XmlNode newXMLNode = XMLDom.SelectSingleNode("/Main node name");

//get the node where you want to insert the data
XmlNode childNode = XMLDom.CreateNode(XmlNodeType.Element,"your node name where you want to insert data","");

//In the below step "name" is your node name and "sree" is your data to insert
XmlAttribute newAttribute = XMLDom.CreateAttribute("name","sree","");
childNode.Attributes.Append(newAttribute);
newXMLNode.AppendChild(childNode);

************************Modify data in xml file************************
// Select childnode where you want to modify data. The following step "PNode" is parent node and "Node1" is the one which you want to update the data

XmlNodeList newXMLNodes = XMLDom.SelectNodes("/PNode/Node1");
foreach(XmlNode newXMLNode in newXMLNodes)

//Updating data where "Node1" is "sree" with "kambham"
if(newXMLNode.InnerText == "sree")
newXMLNode.InnerText = "kambham";
XMLDom.Save("Path of your XML file");
XMLDom = null;
//The above step updates xml file where ever it finds "sree" with "kambham"

************************Delete data in xml file************************

XmlNodeList newXMLNodes = XMLDom.SelectNodes("/PNode/Node1");
foreach(XmlNode newXMLNode in newXMLNodes)
if(newXMLNode.InnerText == "sree")
newXMLNode.ParentNode.RemoveChild(newXMLNode);
XMLDom.Save("Path of your XML file");
XMLDom = null;
//The above step deletes data in xml file where ever it finds "sree"

XML

Here is an XML file named "countries.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>

<countries>

<country>
  <text>Norway</text>
  <value>N</value>
</country>

<country>
  <text>Sweden</text>
  <value>S</value>
</country>

<country>
  <text>France</text>
  <value>F</value>
</country>

<country>
  <text>Italy</text>
  <value>I</value>
</country>

</countries>

<%@ Import Namespace="System.Data" %>

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub
To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

</body>
</html>
Then add the script that builds the XML DataSet:
<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath("countries.xml"))
  rb.DataSource=mycountries
  rb.DataValueField="value"
  rb.DataTextField="text"
  rb.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
</form>

</body>
</html>
Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label:

Example

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath("countries.xml"))
  rb.DataSource=mycountries
  rb.DataValueField="value"
  rb.DataTextField="text"
  rb.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>