Search
Latest topics
CRUD on Xml – LinQ to Xml
Page 1 of 1
CRUD on Xml – LinQ to Xml
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.
Ðầ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.
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Thu Aug 23, 2012 5:38 am by Admin
» Tuyệt kỹ cua giai
Thu Aug 23, 2012 5:36 am by Admin
» NETCAT.........
Mon Aug 13, 2012 6:35 am by Admin
» Bảo mật CSDL bằng phương pháp mã hóa.
Tue Apr 17, 2012 10:04 pm by Admin
» Hàm mã hóa MD5 bằng JavaScript
Tue Apr 17, 2012 10:03 pm by Admin
» Giá của món quà
Fri Apr 13, 2012 6:01 am by Admin
» Sẽ chỉ yêu ai?
Fri Apr 13, 2012 6:01 am by Admin
» Cách đọc bảng chữ cái!
Thu Apr 12, 2012 10:37 pm by Admin
» Gắn trojan, keylog, virus vào website, forum
Tue Apr 10, 2012 1:14 am by Admin