Thông báo
  • Cập nhật giao diện
    25/03/2024

    • Thêm hiệu ứng background chuyển động.
    • Bổ sung widget đồng hồ và lời chúc theo giờ.

  • [BIG UPDATE] Update Median UI 1.7
    28/02/2024

    • Custome header và footer, bổ sung popup Notification và Recent Comments.
    • Tinh chỉnh thêm giao diện trang chủ và font chữ.

  • Thành lập Thành Trương Blog
    19/02/2024

    • Cập nhật giao diện, viết các bài về thông tin Blog
    • Custome một số chỗ đề phù hợp.
    • Đăng bài viết đầu tiên

Nếu bạn gặp khó khăn trong lúc trải nghiệm Blog. Hãy gửi Góp ý và báo lỗi giúp mình nhé

Tìm hiểu về XML trong C# .Net dành cho người mới bắt đầu

xml, c#, csharp, tìm hiểu xml, C#.net, xml cho người mới bắt đầu

Trong quá trình bạn lập trình web, hầu như các bạn đều phải làm việc với JSON hoặc XML. Mặc dù các bạn đã quá quen với việc sử dụng JSON nhưng XML nó không được phổ biến như vậy. Tuy nhiên trong một số trường hợp bắt buộc bạn phải sử dụng đến XML, ví dụ khi mà bạn làm việc cùng 3rd party, và API document require payload và response thì phải dùng đến XML, vì thế bài viết này sẽ giúp bạn hiểu rõ hơn về XML. Mình sẽ đề cập đến mọi thứ bạn cần biết để bắt đầu, từ những điều cơ bản về XML đến cách làm việc với nó.

Tìm hiểu về XML trong C# .Net dành cho người mới bắt đầu
Tìm hiểu về XML trong C# .Net dành cho người mới bắt đầu

XML là gì?

eXtensible Markup Language được viết tắt là XML, đây là một ngôn ngữ đánh dấu được sử dụng để cấu trúc, lưu trữ và trao đổi dữ liệu giữa các ứng dụng và lưu trữ dữ liệu.

Nếu bạn đã quen thuộc với HTML, bạn sẽ thấy XML dễ hiểu vì nó cũng là một ngôn ngữ đánh dấu. XML là một định dạng dựa trên văn bản đơn giản để biểu diễn dữ liệu/thông tin có cấu trúc. Nó được sử dụng trong tài liệu, dữ liệu, cấu hình, giao tiếp giữa các ứng dụng, v.v…

Tuy nhiên, không giống như HTML, bị giới hạn trong các thẻ được xác định trước như p, h1, h2, bảng, v.v., còn đối với XML, bạn có quyền tự do xác định tên thẻ và cấu trúc tài liệu mà không có bất kỳ giới hạn hoặc quy tắc nào.

Bắt đầu với XML với C#

Dưới đây là một ví dụ khi bạn thực hiện với XML:

static XDocument XmlUsingPlainCode()
{
    XDocument document = new XDocument
        (
            new XDeclaration("1.0", "utf-8", "yes"),
            new XComment("XML from plain code"),
            
            new XElement("Courses",
                new XElement("Course",
                    new XElement("Title", "XML Basics"),
                    new XElement("Duration", "15 min"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", "Free")),
                new XElement("Course",
                    new XElement("Title", ".NET Core for Beginners"),
                    new XElement("Duration", "10 hours"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", "Free")))
        );

    return document;
}

XElement cho phép chúng ta tạo một phiên bản mới trực tiếp bên trong XElement. Điều này giúp dễ dàng tạo XElement lồng nhau, cho phép chúng ta tạo cấu trúc XML cụ thể. Bạn hãy xem kết quả bên dưới nhé.

<!--XML from plain code-->
<Courses>
  <Course>
    <Title>XML Basics</Title>
    <Duration>15 min</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price>Free</Price>
  </Course>
  <Course>
    <Title>.NET Core for Beginners</Title>
    <Duration>10 hours</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price>Free</Price>
  </Course>
</Courses>

Sử dụng XAttribute để cung cấp thông tin chi tiết hơn về các phần tử

Để có thể bổ sung thông tin cho một element, bạn có thể dùng đến XAttribute, cụ thể như sau:

static XDocument XmlUsingPlainCode()
{
    XDocument document = new XDocument
        (
            new XDeclaration("1.0", "utf-8", "yes"),
            new XComment("XML from plain code"),
            
            new XElement("Courses",
                new XElement("Course",
                    new XAttribute("Id", 1),
                    new XElement("Title", "XML Basics"),
                    new XElement("Duration", "15 min"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", "Free",
                        new XAttribute("Currency","USD"))),
                new XElement("Course",
                    new XAttribute("Id", 2),
                    new XElement("Title", ".NET Core for Beginners"),
                    new XElement("Duration", "10 hours"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", "Free",
                        new XAttribute("Currency", "DKK"))))
        );

    return document;
}

Trong mỗi XElement, chúng ta có thể khởi tạo một XAttribute mới gần giống như cách chúng ta đã làm với các element. Đây là kết quả:

<!--XML from plain code-->
<Courses>
  <Course Id="1">
    <Title>XML Basics</Title>
    <Duration>15 min</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price Currency="USD">Free</Price>
  </Course>
  <Course Id="2">
    <Title>.NET Core for Beginners</Title>
    <Duration>10 hours</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price Currency="DKK">Free</Price>
  </Course>
</Courses>

Sử dụng LINQ để truy vấn tài liệu

LINQ là một cú pháp truy vấn thống nhất trong C# để truy xuất dữ liệu từ các nguồn và định dạng khác nhau, điều này lý tưởng để sử dụng nếu chúng ta muốn một số dữ liệu cụ thể trong tài liệu XML.

Bây giờ chúng ta sẽ có bài toán sau, chúng ta cần tạo một query để get tất cả các course element, với mỗi course chúng ta chia thành các mệnh giá khác nhau, khi course là Free chúng ta sẽ print "It is a free course", ngược lại ta in giá tiền kèm với currency của nó.

Đầu tiên, các bạn cần update lại mệnh giá của các course.

static XDocument XmlUsingPlainCode()
{
    XDocument document = new XDocument
        (
            new XDeclaration("1.0", "utf-8", "yes"),
            new XComment("XML from plain code"),

            new XElement("Courses",
                // Free Course
                new XElement("Course",
                    new XAttribute("Id", 1),
                    new XElement("Title", "XML Basics"),
                    new XElement("Duration", "15 min"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", "Free",
                        new XAttribute("Currency", "USD"))),

                // Paid Course
                new XElement("Course",
                    new XAttribute("Id", 2),
                    new XElement("Title", "Advanced MS SQL Queries"),
                    new XElement("Duration", "15 hours"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", 12.95,
                        new XAttribute("Currency", "EUR"))),

                // Free Course
                new XElement("Course",
                    new XAttribute("Id", 2),
                    new XElement("Title", ".NET Core for Beginners"),
                    new XElement("Duration", "10 hours"),
                    new XElement("Instructor", "Christian Schou"),
                    new XElement("Price", "Free",
                        new XAttribute("Currency", "DKK"))))
        );

    return document;
}

Bây giờ bạn sẽ phải viết một function sử dụng LINQ.

static void PerformLinqQueryOnXml(string docPath)
{
    XElement document = XElement.Load(docPath); // Load document as an XML Element

    var amountOfCourses = document.Elements("Course"); // Get the course elements into an enumerator

    Console.WriteLine("------ # Extract Courses In Document Using LINQ# ------\n");

    // Write out each course details to the console
    foreach (var course in amountOfCourses)
    {
        Console.WriteLine("### Course Details ###");
        Console.WriteLine($"Course: {course.Element("Title").Value}");
        Console.WriteLine($"Duration: {course.Element("Duration").Value}");
        Console.WriteLine($"Instructor: {course.Element("Instructor").Value}");

        if (course.Element("Price").Value == "Free")
        {
            Console.WriteLine($"Price: It's a free course\n");
        }
        else
        {
            Console.WriteLine($"Price: {course.Element("Price").Attribute("Currency").Value} {course.Element("Price").Value}\n");
        }
    }
}

Bạn đã có thể truy xuất đến element Price bằng cách sử dụng method Element() cộng với .Value để lấy giá trị bên trong element một cách dễ dàng. Cuối cùng, bạn chỉ việc viết condition phù hợp là được.

### Course Details ###
Course: XML Basics
Duration: 15 min
Instructor: Christian Schou
Price: It is a free course

### Course Details ###
Course: .NET Core for Beginners
Duration: 10 hours
Instructor: Christian Schou
Price: EUR 12.95

### Course Details ###
Course: .NET Core for Beginners
Duration: 10 hours
Instructor: Christian Schou
Price: It is a free course

Serialize XML

Khi bạn cần serializing objects thì .NET framework đã cung cấp đến cho bạn một công cụ để bạn có thể dễ dàng tuần tự hóa XML bất kì object nào.

Để thực hiện thì mình đã tạo một thư mục mới có tên Models. Trong thư mục đó, mình đã thêm 3 lớp mới có tên như sau:

  • Courses.cs
  • Course.cs
  • Price.cs

Dưới đây là mỗi class được định nghĩa như sau.

Courses.cs

using System.Collections.Generic;
using System.Xml.Serialization;

namespace XMLBasics.Models
{
    [XmlRoot("Courses")]
    public class Courses
    {
        [XmlElement(ElementName = "Academy")]
        public string Academy { get; set; }

        [XmlElement(ElementName = "Course")]
        public List<Course> CourseList { get; set; }

        public Courses()
        {
            CourseList = new List<Course>();
        }
    }
}

Course.cs

using System.Xml.Serialization;

namespace XMLBasics.Models
{
    [XmlRoot(ElementName = "Course")]
    public class Course
    {
    [XmlElement(ElementName = "Title")]
    public string Title { get; set; }

    [XmlElement(ElementName = "Duration")]
    public string Duration { get; set; }

    [XmlElement(ElementName = "Instructor")]
    public string Instructor { get; set; }

    [XmlElement(ElementName = "Price")]
    public Price Price { get; set; }

    [XmlAttribute(AttributeName = "Id")]
    public int Id { get; set; }

    [XmlText]
    public string Text { get; set; }

        public Course()
        {
        Price = new Price();
        }
    }
}

Price.cs

using System.Xml.Serialization;

namespace XMLBasics.Models
{
    [XmlRoot(ElementName = "Price")]
    public class Price
    {
        [XmlAttribute(AttributeName = "Currency")]
        public string Currency { get; set; }

        [XmlText]
        public string Text { get; set; }
    }
}

Trên mỗi field, mỗi models, bạn đã chỉ định các XML attributes. Bạn có thể thấy chúng sẽ có các tên khác nhau vì muốn nó hoạt động khác nhau khi serialize các đối tượng thành XML.

Với mỗi class, sẽ luôn bắt đầu với việc định nghĩa XML root là gì ứng với một tên phần tử. Bạn có thể đặt tên cho model, sau đó thay đổi tên của nó tại thời điểm serialization. Sau đó, bạn chỉ định tất cả các phần tử thuộc thuộc tính cụ thể đó trong document.

Tiếp tục phải tạo một số objects mới và đặt chúng vào một list. List này sau đó sẽ được chuyển thành XML bằng thư viện System.Xml.Serialization. Sau cùng, chúng ta tạo một phương thức SerializeToXml() với code sau:

static string SerializeToXml()
{
    string instructor = "Christian Schou";
    int i = 1;
    Random rand = new Random();

    // Create some courses
    Courses courses = new Courses { Academy = "Coding with Christian Schou",
        CourseList = new List<Course> {
            new Course
            {
                Title = "How to get started with MailKit",
                Duration = $"{rand.Next(1, 5)} hours and {rand.Next(0, 60)} minutes",
                Instructor = instructor,
                Price = new Price { Currency = "DKK", Text = "Free" }
            },
            new Course
            {
                Title = "Dapper with repository in ASP.NET",
                Duration = $"{rand.Next(1, 5)} hours and {rand.Next(0, 60)} minutes",
                Instructor = instructor,
                Price = new Price { Currency = "EUR", Text = "Free" }
            },
            new Course
            {
                Title = "How to create a WEB API",
                Duration = $"{rand.Next(1, 5)} hours and {rand.Next(0, 60)} minutes",
                Instructor = instructor,
                Price = new Price { Currency = "USD", Text = "19.95" }
            }
        }
    };

    // Set ID on each Course

    foreach (var course in courses.CourseList)
    {
        course.Id = i++;
    }

    XmlSerializer x = new XmlSerializer(courses.GetType()); // Get the exact runtime type of the current instance
    StringBuilder output = new StringBuilder();
    var writer = new StringWriter(output);
    x.Serialize(writer, courses);

    return output.ToString();

}

Method này sẽ tạo các console line nhắm xác định dữ liệu khi chạy. Chúng ta đã thêm ba course làm đối tượng và thêm chúng vào danh sách được đặt tên courses dựa trên model Courses.cs. Đối với mỗi course, các bạn sẽ cung cấp cho nó một ID. Điều này có thể được thực hiện theo nhiều cách, nhưng cách đơn giản là dùng một vòng lặp foreach đơn giản giống như vòng lặp ở trên. Nó chỉ cần lấy từng courses và cập nhật ID với số mà courses có trong danh sách.

Bạn sẽ phải khởi tạo một instance mới của XmlSerializer nơi chỉ định loại danh sách có các courses. Cuối cùng, serialize content bằng cách sử dụng XmlSerializer. Như vậy, bạn sẽ được kết quả như sau:

<?xml version="1.0" encoding="utf-16"?>
<Courses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Academy>Coding with Christian Schou</Academy>
  <Course Id="1">
    <Title>How to get started with MailKit</Title>
    <Duration>4 hours and 42 minutes</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price Currency="DKK">Free</Price>
  </Course>
  <Course Id="2">
    <Title>Dapper with repository in ASP.NET</Title>
    <Duration>4 hours and 30 minutes</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price Currency="EUR">Free</Price>
  </Course>
  <Course Id="3">
    <Title>How to create a WEB API</Title>
    <Duration>4 hours and 38 minutes</Duration>
    <Instructor>Christian Schou</Instructor>
    <Price Currency="USD">19.95</Price>
  </Course>
</Courses>

Kết luận

Trên đây là bài viết giúp bạn tìm hiểu về XML trong C# .Net, nếu bạn là người mới bắt đầu tìm hiểu XML thì hãy xem bài viết này nhé. Nếu bạn có bất kỳ câu hỏi hoặc khiếu nại về bản quyền, hãy để lại ý kiến của bạn bên dưới để mình biết. Chúc bạn có một ngày học tập và làm việc hiệu quả.

Thông tin liên hệ:

Thành Trương Blog

Chịu trách nhiệm nội dung: Nguyễn Thành Trương

Địa chỉ: Xã Tân Thành, Huyện Bình Tân, Tỉnh Vĩnh Long

Email: thanhtruongblog@gmail.com

Số điện thoại: 0973.898.830

Website: https://www.thanhtruongblog.com/

Nguồn: https://www.thanhtruongblog.com/2024/02/tim-hieu-ve-xml-trong-csharp-net-danh-cho-nguoi-moi-bat-dau.html

Bài viết liên quan:
Blogger tự do!
Bình luận