HACKIS - Hacking Internet Security
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» Tuyệt Kỹ Đong Giai Chân Kinh (tuyệt Kỹ cua trai)
CRUD on Xml – LinQ to Xml EmptyThu Aug 23, 2012 5:38 am by Admin

» Tuyệt kỹ cua giai
CRUD on Xml – LinQ to Xml EmptyThu Aug 23, 2012 5:36 am by Admin

» NETCAT.........
CRUD on Xml – LinQ to Xml EmptyMon Aug 13, 2012 6:35 am by Admin

» Bảo mật CSDL bằng phương pháp mã hóa.
CRUD on Xml – LinQ to Xml EmptyTue Apr 17, 2012 10:04 pm by Admin

» Hàm mã hóa MD5 bằng JavaScript
CRUD on Xml – LinQ to Xml EmptyTue Apr 17, 2012 10:03 pm by Admin

» Giá của món quà
CRUD on Xml – LinQ to Xml EmptyFri Apr 13, 2012 6:01 am by Admin

» Sẽ chỉ yêu ai?
CRUD on Xml – LinQ to Xml EmptyFri Apr 13, 2012 6:01 am by Admin

» Cách đọc bảng chữ cái!
CRUD on Xml – LinQ to Xml EmptyThu Apr 12, 2012 10:37 pm by Admin

» Gắn trojan, keylog, virus vào website, forum
CRUD on Xml – LinQ to Xml EmptyTue Apr 10, 2012 1:14 am by Admin

Affiliates
free forum


CRUD on Xml – LinQ to Xml

Go down

CRUD on Xml – LinQ to Xml Empty CRUD on Xml – LinQ to Xml

Post  Admin Thu Mar 29, 2012 1:02 am

Bài viết này mình muốn giới thiệu cho các bạn vài thao tác thêm,xóa,sửa trên XML, sử dụng Linq to Xml.

Ðầu tiên chúng ta sẽ tạo ra 1 file Xml.
Ðây là đoạn code tạo file Xml :

XDocument xdoc = new XDocument
(
// Chế độ chứng thực.
new XDeclaration(“1.0″,”UTF-8″,”Yes”),
// Khởi tạo Root.
new XElement(“User”,
// Khỏi tạo Element ID.
new XElement(“ID”
// Khởi tạo các node con.
,new XAttribute(“type”,”Author”)
,new XElement(“FirstName”,”Van”)
,new XElement(“LastName”,”A”))
,new XElement(“ID”
,new XAttribute(“type”,”Editor”)
,new XElement(“FirstName”,”Nguyen”)
,new XElement(“LastName”,”B”))
,new XElement(“ID”
, new XAttribute(“type”,”Admin”)
,new XElement(“FirstName”,”Ta”)
,new XElement(“LastName”,”C”)))
);
Console.WriteLine(xdoc);
// Save file Xml.
xdoc.Save(Environment.CurrentDirectory + @”\user.xml”);

Đây là file Xml ta tạo ra.

Thêm element mới.

Thêm vào đầu file Xml sau Root.

new XElement id = xdoc.Descendants(“User”).Last();
id.AddFirst(new XElement(“ID”
, new XAttribute(“type”, “Writer”)
, new XElement(“FirstName”, “Phan”)
, new XElement(“LastName”, “Z”)));

hoặc

xdoc.Root.AddFirst(new XElement(“ID”
, new XAttribute(“type”, “Writer”)
, new XElement(“FirstName”, “Phan”)
, new XElement(“LastName”, “Z”)));

- Thêm vào cuối file Xml.

XElement id = xdoc.Descendants(“User”).Last();
id.Add(new XElement(“ID”
, new XAttribute(“type”, “Writer”)
, new XElement(“FirstName”, “Phan”)
, new XElement(“LastName”, “Z”)));

- Thêm vào sau Element được chỉ định (tương tự như thêm vào trước ta chỉ cần sử dụng phương thức AddBeforeSelf).

xdoc.Element(“User”).Elements(“ID”).
Where(ele => ele.Element(“FirstName”).Value == “Nguyen”).
Single().AddAfterSelf
(
new XElement(“ID”
, new XAttribute(“type”, “Director”)
, new XElement(“FirstName”, “Pham”)
, new XElement(“LastName”, “X”))
);

Chỉnh sửa các Element

Ta sử dụng phương thức :
- SetElementValue để sửa value của các Element
- SetAttributeValue để sửa Attribute của các Element

XElement xeles = (from s in xdoc.Descendants(“ID”)
where s.Element(“FirstName”).Value == “Nguyen”
select s).SingleOrDefault();
xeles.SetAttributeValue(“type”, “AnBamer”);
xeles.SetElementValue(“FirstName”, “Vo”);

Ta cũng có thể sử dụng Xpath để chỉnh sửa

XElement xele = xdoc.XPathSelectElement(“//User/ID[FirstName='Nguyen']“);
xele.SetElementValue(“FirstName”, “Ng”);

Xóa các Element

- Xóa Element với node được chỉ định

xdoc.Descendants(“ID”).Where(ele => ele.Element(“FirstName”).Value == “Vo”).Remove();

- Xóa tất cả các Element trong tập tin xml

xdoc.Element(“User”).RemoveAll();

Trên đây chỉ là những ví dụ cơ bản về các thao tác thêm, xóa, sửa trong Xml sử dụng LinQ to Xml.
Hi vọng bài viết này sẽ giúp ích cho các bạn trong khi làm việc với các tập tin xml.
Admin
Admin
Admin

Tổng số bài gửi : 782
Join date : 2009-08-15

https://hackis.forumvi.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum