About us |  Help us improve
intelliVERB - Get a fair search of the Internet !
Visual studio .net
SQL Server
Oracle
Main  >  XML Email this page to friend
Document sample
SAX parser
Xml and Datasets
XmlReader
XmlShema
XmlWriter
XPathNavigator
 
 
 
 
 
C#Document sampleTop

<?xml version="1.0" encoding="utf-8" ?>
<employees>
 <employee ID="0001001">
  <!--ID here represent an attribute for the node employee-->
  <!--FirstName represent an element-->
  <FirstName>Kevin</FirstName>
  <LastName>Smith</LastName>
  <Position>Office manager</Position>
  <Salary>40000</Salary>
  <DateOfHire>05/06/2000</DateOfHire>
  <!--Street, City, State, Zip are all attribute of element Address-->
  <Address Street="1245 First st" City="City Ville" State="CA" Zip="90265"/>
 </employee>
 <employee ID="0054001">
  <FirstName>Eva</FirstName>
  <LastName>Brooks</LastName>
  <Position>Accountant</Position>
  <Salary>52000</Salary>
  <DateOfHire>11/01/2001</DateOfHire>
  <Address Street="784 Max Stern st" City="Litle Place" State="CA" Zip="90242"/>
 </employee>
</employees>



C#XmlReaderTop

Using System;
Using System.Xml;

namespace DoAllApp
{
 public class XmlReaderClass
 {
  public static bool ReadSampleDoc()
  {
   XmlTextReader myReader=new XmlTextReader("SampleXML.xml");
   //read the document node after node
   while (myReader.Read())
   {
    if (myReader.NodeType==XmlNodeType.Element)
    {
     Console.Write(myReader.Name);
     //read the node attribute after attribute
     while(myReader.MoveToNextAttribute())
     {
      Console.Write(myReader.Name+": "+myReader.Value);
     }
    }
    else if (myReader.NodeType==XmlNodeType.EndElement)
    {
     Console.WriteLine("--------------------");
    }
   }
   return true;
  }
 }
}


TIPTop
DTD: document type definition
XmlTextReader derived from XmlReader and implement methods defined by it.

C#XmlWriterTop

Using System.Xml;

namespace DoAllApp
{
 
 public class XmlWriterClass
 {
  
  public static bool WriteMyDoc()
  {
   //the document will be create with UTF-8 character encoding
   XmlTextWriter MyWriter=new XmlTextWriter(@"c:\Temp\MyDoc.xml",System.Text.Encoding.UTF8);
   //Set all child element to be indented
   MyWriter.Formatting=Formatting.Indented;
   //Writes the XML declaration with the version "1.0".
   MyWriter.WriteStartDocument(false);
   MyWriter.WriteDocType("Employees",null,null,null);
   //<employees>
   MyWriter.WriteStartElement("employees");
   //<employee ID="0124210">
   MyWriter.WriteStartElement("employee");
   MyWriter.WriteAttributeString("ID","0124210");
   //<FirstName>James</FirstName>
   MyWriter.WriteElementString("FirstName","James");
   //<LastName>Thomas</LastName>
   MyWriter.WriteElementString("FirstName","Thomas");
   //<Position>Customer Service</Position>
   MyWriter.WriteElementString("Position","Customer Service");
   //<Salary>40000</Salary>
   MyWriter.WriteElementString("Salary","40000");
   //<DateOfHire>05/06/2000</DateOfHire>
   MyWriter.WriteElementString("DateOfHire","05/06/2000");
            //<Address Street="145 Fenton st" City="Silver Spring" State="MD" Zip="20902"/>
   MyWriter.WriteElementString("Address",null);
   MyWriter.WriteAttributeString("Street","145 Fenton st");
   MyWriter.WriteAttributeString("City","Silver Spring");
   MyWriter.WriteAttributeString("State","MD");
   MyWriter.WriteAttributeString("Zip","20902");
   //</employee>
   MyWriter.WriteEndElement();
   //Restart building the next employee node
   //............//
   //</employees>

   MyWriter.WriteEndElement();
   //Write the Xml document to the target file
   MyWriter.Flush();
   //close MyWriter
   MyWriter.Close();
   return true;
  }

 }
}


C#XPath, XPathNavigatorTop

Using System;
Using System.Xml;
Using System.Xml.XPath;
namespace DoAllApp
{
 /// <summary>
 /// Summary description for XPath.
 /// </summary>

 public class XPathClass
 {
  public static bool ReadSampleDoc()
  {
   string MyXML="<?xml version=\"1.0\" ?> <Students>"+
    "<Total key=\"Presents\" value=\"15\"/>"+
    "<Total key=\"Absents\" value=\"1\"/>"+
    "<Student>"+
    "<Id>34501</Id>"+ 
    "<FirstName>Jack</FirstName>"+
    "<LastName>Doe</LastName>"+
    "<Grade>3rd</Grade>"+
    "<LunchMoney Used=\"$25\" Balance=\"$5\" />"+
    "</Student>"+
    "<Student>"+
    "<Id>34521</Id>"+ 
    "<FirstName>Brandon</FirstName>"+
    "<LastName>Terrell</LastName>"+
    "<Grade>3rd</Grade>"+
    "<LunchMoney Used=\"$35\" Balance=\"$0\" />"+
    "</Student>"+
    "</Students>";

   XmlDocument myDocument=new XmlDocument();
   myDocument.LoadXml(MyXML);
   XPathNavigator myNavigator=myDocument.CreateNavigator();
   XPathNodeIterator myNode=myNavigator.Select("/Students/Student");
   myNode.MoveNext();
   string Name=myNode.Current.Name;
   //Name=Student
   string Value=myNode.Current.Value;
   //Value=34501JackDoe3rd
   //reading using XPath
   int .TotalStudentPresent=Convert.ToInt32(myDocument.SelectSingleNode("/Students/Total[@key='Presents']/@value").InnerText);
   //TotalStudentPresent=15
   int .TotalStudentAbsent=Convert.ToInt32(myDocument.SelectSingleNode("/Students/Total[@key='Absents']/@value").InnerText);
   //TotalStudentAbsent=1
   
   //select all student's with lunch money =$0
   XmlNodeList xNodeList = myDocument.SelectNodes("descendant::Students/Student/LunchMoney[@Balance='$0']");
   foreach. (XmlNode xN in xNodeList)
   {
    //set lunch money balance to $10
    xN.Attributes["Balance"].Value="$10";
    //Brandon Terrell Lunch money balance is set to $10
   }
   return true;
  }

 }
}


C#XmlShemaTop

To be implemented


C#Xml and DatasetsTop

To be implemented


C#SAX parserTop

To be implemented

 
 
Make a secure donation now with PayPal!