Professional Documents
Culture Documents
Guoi Tao
Guoi Tao
2.Những đặc tính của XML được tổ chức W3c đề nghị gồm những
-Những tag không định nghĩa trước
-Kết thúc một tag mở là một tag đóng
-Có phân biệt chữ hoa và chữ thường
-Những tag có thể lòng vào nhău
-Một file xml chỉ có một root element
-Chú thích(comment) tương tự như HTML
-Các node cha và con có mối tương quan giửa các thành phần
-Kiểu nội dung trong mổi thành phần là khác nhău
-tên được đặt Tuân theo các quy tắc đặt tên thông thường khác
XML đơn giản, độc lập với nền tảng , cấu trúc dữ liệu tách biệt với
Giao diện người dùng. XML có thể được dùng để quốc tế hóa
Một ứng dụng .NET bởi vì nó dựa vào định dạng UNICODE
<?xml version="1.0"?>
<Persons>
<PersonID ID="ID1">
<Name>Jane Doe</Name>
<Address1>LeNin Part</Address1>
<Address2>London street</Address2>
<City>New York</City>
<Salary>500</Salary>
</PersonID>
<PersonID ID="ID2">
<Name>tinh</Name>
<Address1>125 box</Address1>
<Address2>34 any</Address2>
<City>New York</City>
<Salary>6788</Salary>
<Phoneno>012541334</Phoneno>
</PersonID>
</Persons>
namespace XML1
class XML {
//create instance
xmlwt.Formatting = Formatting.Indented;
xmlwt.Indentation = 6;
xmlwt.WriteStartDocument();
xmlwt.WriteStartElement("","Persons","");//start person
xmlwt.WriteStartAttribute("ID");
xmlwt.WriteString("ID1");
xmlwt.WriteEndAttribute();
xmlwt.WriteString("Jane Doe");
xmlwt.WriteEndElement();
xmlwt.WriteString("LeNin Part");
xmlwt.WriteEndElement();
xmlwt.WriteString("London street");
xmlwt.WriteEndElement();
xmlwt.WriteString("New York");
xmlwt.WriteEndElement();
xmlwt.WriteStartElement("", "Salary", "");
xmlwt.WriteValue(456);
xmlwt.WriteEndElement();
xmlwt.WriteStartAttribute("ID");
xmlwt.WriteString("ID2");
xmlwt.WriteEndAttribute();
xmlwt.WriteString("JMaris");
xmlwt.WriteEndElement();
xmlwt.WriteString("125 box");
xmlwt.WriteEndElement();
xmlwt.WriteString("34 any");
xmlwt.WriteEndElement();
xmlwt.WriteString("New York");
xmlwt.WriteEndElement();
xmlwt.WriteStartElement("", "Salary", "");
xmlwt.WriteValue(1235);
xmlwt.WriteEndElement();
xmlwt.WriteEndElement();
xmlwt.WriteEndElement();
xmlwt.Flush();
xmlwt.Close();
while (xmlr.Read()) {
if (xmlr.NodeType == XmlNodeType.Element) {
if(xmlr.LocalName.Equals("Name")){
Console.WriteLine("Name of person{0}
",xmlr.ReadString());
if(xmlr.LocalName.Equals("Address1")){
Console.WriteLine("Address1 of person {0} ",
xmlr.ReadString());
if (xmlr.LocalName.Equals("Address2"))
if (xmlr.LocalName.Equals("City"))
Console.WriteLine("City of person{0}:
",xmlr.ReadString());
if (xmlr.LocalName.Equals("Salary"))
Console.WriteLine("Salary of person{0}:
",xmlr.ReadString());
Console.ReadLine();
sử dụng Xpath để truy cập đến từng nod của file XML
node.Load(@"D:\xmlFile.txt");
// using xmlNode
Console.WriteLine(nodename.InnerText);
Console.WriteLine(nodename1.InnerText);
Console.WriteLine();
Console.WriteLine(nodes.InnerText+" "+"\n");
XmlNodeList nodelistid =
rootnode.SelectNodes(".//PersonID[@ID='ID1']");
//----------------------------->>>
XmlNodeList nodelistidsa =
rootnode.SelectNodes(".//PersonID[Salary<344]");
Chỉnh sửa một node bất kỳ từ file xml đã được tạo ở trên
//string path;
//edit.Load(@"D:\xmlFile.txt");
//XmlNode node;
//node = edit.DocumentElement;
// if (nodwtwo.Name == "City")
// {
// if (nodwtwo.InnerText == "JMaris")
// {
// string newname;
// newname = Console.ReadLine();
// nodwtwo.InnerText = newname;
// }
// }
//edit.Save(@"D:\xmlFile.txt");
mydoc.Load(filename);
XmlElement root=mydoc.DocumentElement;
XmlNode mynode;
mynode =
root.SelectSingleNode("/Persons/PersonID[@ID='ID2']/Name");
mynode.InnerText = "tinh";
mydoc.Save(@"D:\xmlFile.txt");
Console.WriteLine("edit is sucessfuly");
string filename=@"D:\xmlFile.txt";
mydoc.Load(filename);
newelement.InnerText="012541334";
XmlNode oddnode;
oddnode = root.SelectSingleNode("/Persons/PersonID[@ID='ID2']");
oddnode.AppendChild(newelement);
mydoc.Save(@"D:\xmlFile.txt");
lớp để triệu gọi các phương thức chứa hàm main để thực thi chương trình
class Program
objXML.writeXml();
objXML.readXml();
objXML.xmlNode();
objXML.usingXpath();
objXML.editXml();
objXML.addnew();
Console.ReadKey();