Tuesday, 31 December 2013

Enter Data in XML File Based on Customer Requirements



Example : Adding Element  in Customer.XML File Based on Customer Requirements


If customer want to add Name, Address and City then write below code.



Step 1 : First Create XML file named of customers.xml

Step 2 : Enter Data in customers.xml as follows.

<Customers>
  
  <Customer>
    <Name>Satyesh</Name>
    <Address>12/C, Yogjivan Society-1,Waghodia Road</Address>
    <City>Baroda</City>
  </Customer>

</Customers>

Step 3: Put 3 textbox and give named of 'txtname', 'txtadd' and 'txtcity' and put one button named of 'btnNewData' on default.aspx page and write code on button click event as follows.

private void btnNewData_Click(object sender, System.EventArgs e)
{
            String name = txtname.Text;
String add = txtadd.Text;
String city = txtcity.Text;

XmlDocument docXML = new XmlDocument();
            docXML.Load(server.mapPath("~/customers.xml"));
XmlElement elmXML = docXML.CreateElement("Customer");//Create Root Element
            string strNewPart =  "<Name>" + name + "</ Name >" +
                                            "<Address>" + add + "</Adderss>" +
                                             "<City>" + city + "</City>";

            elmXML.InnerXml = strNewPart;
            docXML.DocumentElement.AppendChild(elmXML); //Add New Child Element in customer.xml file
            docXML.Save(server.mapPath("~/"customers.xml"));
}


finally, user entered data stored in customers.xml file when you clicked on button.

enjoy................

Thanks To,

www.shreeinfotech.ind.in 

XML Basic, XML Data Load in GridView Step by Step in ASP.Net



XML stands for Extensible Markup Language. 

You can define your own tags. And you can store your data in structured format. Unlike HTML, XML stores and exchanges data. By contrast, HTML represents the data. You can create separate XML files to store data, which can be used as a data source for HTML and other applications.

How Can XML be Used?

1)XML Separates Data from HTML
If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.

With XML, data can be stored in separate XML files. This way you can concentrate on using HTML/CSS for display and layout, and be sure that changes in the underlying data will not require any changes to the HTML.
With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.

2) XML Simplifies Data Sharing
In the real world, computer systems and databases contain data in incompatible formats.
XML data is stored in plain text format. This provides a software- and hardware-independent way of storing data.
This makes it much easier to create data that can be shared by different applications.

3)XML Simplifies Data Transport
One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet.
Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.

4)XML Simplifies Platform Changes
Upgrading to new systems (hardware or software platforms), is always time consuming. Large amounts of data must be converted and incompatible data is often lost.
XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.



XML Syntax Rules :



1)All XML Elements Must Have a Closing Tag

 In HTML, some elements do not have to have a closing tag:

<p>This is a paragraph.
<br>



In XML, it is illegal to omit the closing tag. All elements must have a closing tag:

<p>This is a paragraph.</p>
<br />


Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag.



2)XML Tags are Case Sensitive

XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.

Opening and closing tags must be written with the same case:

<Message>This is incorrect</message>
<message>This is correct</message>

Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is exactly the same thing.



3)XML Elements Must be Properly Nested

In HTML, you might see improperly nested elements:

<b><i>This text is bold and italic</b></i>

In XML, all elements must be properly nested within each other:

<b><i>This text is bold and italic</i></b>

In the example above, "Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.



4)XML Documents Must Have a Root Element

XML documents must contain one element that is the parent of all other elements. This element is called the rootelement.

<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>



5)XML Attribute Values Must be Quoted

XML elements can have attributes in name/value pairs just like in HTML.

In XML, the attribute values must always be quoted.

Study the two XML documents below. The first one is incorrect, the second is correct:

<note date=31/01/2013>
  <to>Shree</to>
  <from>Infotech</from>
</note>



<note date="01/01/2014">
  <to>Shree</to>
  <from>Infotech</from>
</note>

The error in the first document is that the date attribute in the note element is not quoted.



6)Entity References

Some characters have a special meaning in XML.

If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.



This will generate an XML error:

<message>if salary < 1000 then</message>



To avoid this error, replace the "<" character with an entity reference:

<message>if salary &lt; 1000 then</message>

There are 5 predefined entity references in XML:

&lt;
< 
less than
&gt;
> 
greater than
&amp;
&
ampersand 
&apos;
'
apostrophe
&quot;
"
quotation mark

Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.



7)Comments in XML

The syntax for writing comments in XML is similar to that of HTML.

<!-- This is a comment -->



8)White-space is Preserved in XML

HTML truncates multiple white-space characters to one single white-space:

HTML:
Hello           Shree
Output:
Hello Shree

With XML, the white-space in a document is not truncated.

Example : Load XMLFile.XML into GridView. 

Step:1 First create one XML File as below.


 Step:2 Put Button and Gridview on Default.aspx page as follows.


Step:3 Double click on button and write down code on button_click events as follows.


Step:4 Run Website and click on Button so it will display record in gridview as follows.


Enjoy XML code !!!!!!!!!!!!!!!!!!!!!!!!!