XML Parser
利用XML DOM创建XML Parser,XML DOM类是XML文件在内存中的表示形式。DOM类用来读取并操作XML文档。.NET框架提供了可在XML文档中导航并获得相关信息的类。
.NET中还可以采用SAX(Simple API for XML)从XML文档中读取数据,SAX并不将整个XML文档载入内存,比较适合读取大型XML文档,不过SAX不能维护任何执行复杂搜索所需的数据结构,要想修改XML文档不能使用SAX。
还可以采用XmlReader类使用XML文档,XmlReader类提供了对XML文档的只读和只向前访问,不将XML文档载入内存,XmlWriter类用来向文件中写入XML。
xmlDOM.cs
#region
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
#endregion
namespace CSharp_XMLSample
{
public class XmlDOM
{
[STAThread]
static void Main(string[] args)
{
//Xml分析器,读取Xml文档并将内容显示在控制台窗口中
XmlParser myXmlParser = new XmlParser();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
#endregion
namespace CSharp_XMLSample
{
public class XmlDOM
{
[STAThread]
static void Main(string[] args)
{
//Xml分析器,读取Xml文档并将内容显示在控制台窗口中
XmlParser myXmlParser = new XmlParser();
}
}
}
xmlParser.cs
#region
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
#endregion
public class XmlParser
{
public XmlParser()
{
try
{
XmlDocument myDocument = new XmlDocument();
string xmlData;
myDocument.Load("emp.xml");
int i = 1;
int count = 0;
XmlNode node = myDocument.ChildNodes[1];
foreach (XmlNode node1 in node.ChildNodes)
{
Console.WriteLine("\n");
Console.WriteLine("The elements under node number:{0}", i);
Console.WriteLine("------------------------------------");
foreach (XmlNode node2 in node1.ChildNodes)
{
Console.WriteLine(myDocument.DocumentElement.FirstChild.ChildNodes[count].Name + ":" + node2.FirstChild.Value);
count += 1;
}
i += 1;
count = 0;
}
Console.WriteLine("\n");
Console.WriteLine("Press <Enter> to quit");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
#endregion
public class XmlParser
{
public XmlParser()
{
try
{
XmlDocument myDocument = new XmlDocument();
string xmlData;
myDocument.Load("emp.xml");
int i = 1;
int count = 0;
XmlNode node = myDocument.ChildNodes[1];
foreach (XmlNode node1 in node.ChildNodes)
{
Console.WriteLine("\n");
Console.WriteLine("The elements under node number:{0}", i);
Console.WriteLine("------------------------------------");
foreach (XmlNode node2 in node1.ChildNodes)
{
Console.WriteLine(myDocument.DocumentElement.FirstChild.ChildNodes[count].Name + ":" + node2.FirstChild.Value);
count += 1;
}
i += 1;
count = 0;
}
Console.WriteLine("\n");
Console.WriteLine("Press <Enter> to quit");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
emp.xml
<?xml version="1.0"?>
<employees>
<employee>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<DateOfBirth>08/09/1968</DateOfBirth>
<DateOfJoining>04/01/1992</DateOfJoining>
<Address>2010 Stanley Dr.,Charlotte,NC 28273</Address>
<Basic>2100</Basic>
<Desingnation>Associate Consultant</Desingnation>
<LeaveBalance>12</LeaveBalance>
</employee>
<employee>
<FirstName>Luis</FirstName>
<LastName>Bonifaz</LastName>
<DateOfBirth>01/12/1972</DateOfBirth>
<DateOfJoining>06/01/2000</DateOfJoining>
<Address>7862 Freepoint Pkwy,Tampa,FL 33624</Address>
<Basic>1400</Basic>
<Desingnation>Developer</Desingnation>
<LeaveBalance>4</LeaveBalance>
</employee>
</employees>
<employees>
<employee>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<DateOfBirth>08/09/1968</DateOfBirth>
<DateOfJoining>04/01/1992</DateOfJoining>
<Address>2010 Stanley Dr.,Charlotte,NC 28273</Address>
<Basic>2100</Basic>
<Desingnation>Associate Consultant</Desingnation>
<LeaveBalance>12</LeaveBalance>
</employee>
<employee>
<FirstName>Luis</FirstName>
<LastName>Bonifaz</LastName>
<DateOfBirth>01/12/1972</DateOfBirth>
<DateOfJoining>06/01/2000</DateOfJoining>
<Address>7862 Freepoint Pkwy,Tampa,FL 33624</Address>
<Basic>1400</Basic>
<Desingnation>Developer</Desingnation>
<LeaveBalance>4</LeaveBalance>
</employee>
</employees>