---
title: Xml.Document – แปลง XML เป็นโครงสร้างตาราง
url: https://www.thepexcel.com/functions/power-query/accessing-data-functions/xml-document/
type: function-explainer
program: Power Query
syntax: "Xml.Document(contents as any, optional encoding as nullable number) as table"
date: 2025-12-12
updated: 2025-12-23
scores:
  popularity: 6
  difficulty: 4
  usefulness: 6
---

# Xml.Document – แปลง XML เป็นโครงสร้างตาราง

> Xml.Document ใช้สำหรับแปลงเนื้อหา XML (text หรือ binary) เป็นโครงสร้างตารางลำดับชั้น ทำให้สามารถ que

## คำอธิบาย

Xml.Document ใช้สำหรับแปลงเนื้อหา XML (text หรือ binary) เป็นโครงสร้างตารางลำดับชั้น ทำให้สามารถ query ข้อมูล XML ได้ง่ายเหมือน Power Query ตาราง

## Syntax

```excel
Xml.Document(contents as any, optional encoding as nullable number) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| contents | Yes | any |  | เนื้อหา XML ที่ต้องการแปลง สามารถเป็น text string หรือ binary data |
| encoding | No | nullable number | null | รหัสการเข้ารหัส (encoding) ที่ใช้ในการแปลง XML เช่น 1200 สำหรับ UTF-16, 65001 สำหรับ UTF-8 |

## ตัวอย่าง

### 1. แปลง XML string พื้นฐาน

```excel
let
    XmlString = "<Root><Item><Name>Product A</Name><Price>100</Price></Item><Item><Name>Product B</Name><Price>200</Price></Item></Root>",
    ParsedXml = Xml.Document(XmlString)
in
    ParsedXml
```

**ผลลัพธ์:** `ตารางแสดงโครงสร้าง Root element พร้อม child nodes`

Xml.Document เปลี่ยน XML string เป็นตารางลำดับชั้น แต่ละแถวแสดง element ที่พบ

### 2. กรอง element เฉพาะที่ต้องการ

```excel
let
    XmlContent = File.Contents("data.xml"),
    ParsedXml = Xml.Document(XmlContent),
    Items = Table.SelectRows(ParsedXml, each [Name] = "Item")
in
    Items
```

**ผลลัพธ์:** `ตารางที่มีเฉพาะ Item elements`

ผสม Xml.Document กับ Table.SelectRows เพื่อกรอง element ที่ต้องการออกมา

### 3. ขยาย nested elements

```excel
let
    XmlString = "<Root><Item><Name>Product A</Name><Price>100</Price></Item></Root>",
    ParsedXml = Xml.Document(XmlString),
    Expanded = Table.ExpandRecordColumn(ParsedXml, "Item", {"Name", "Price"}, {"Name", "Price"})
in
    Expanded
```

**ผลลัพธ์:** `ตารางที่มีคอลัมน์ Name และ Price แยกออกมา`

ใช้ Table.ExpandRecordColumn เพื่อเปิดตัว nested record ออกมาเป็นคอลัมน์

### 4. ระบุ encoding สำหรับ XML ที่เข้ารหัสเป็น UTF-16

```excel
let
    BinaryXml = File.Contents("data-utf16.xml"),
    ParsedXml = Xml.Document(BinaryXml, 1200)
in
    ParsedXml
```

**ผลลัพธ์:** `ตารางแสดงโครงสร้าง XML ที่อ่านได้ถูกต้อง`

พารามิเตอร์ encoding ช่วยให้สามารถจัดการ XML ที่เข้ารหัสแตกต่างกัน (1200 = UTF-16, 65001 = UTF-8)

## หมายเหตุเพิ่มเติม

- Xml.Document ส่งกลับ table ที่อาจมี nested record columns โปรดทราบว่าต้องขยาย (expand) เพื่อเข้าถึงข้อมูลลึกลงไป

- ใช้ File.Contents เมื่อ load XML จากไฟล์ หรือ Text.ToBinary เมื่อแปลง text เป็น binary ก่อน

- กำหนด encoding ถ้ารู้ว่า XML ใช้ encoding ที่ไม่ใช่ default (เช่น UTF-16 หรือ Big5)

- ผสมกับ Table.SelectRows และ Table.ExpandRecordColumn เพื่อทำให้ XML transformation เป็นไปได้อย่างยืดหยุ่น

## คำถามที่พบบ่อย

**Q: ผลลัพธ์จาก Xml.Document คืออะไร?**

ผลลัพธ์คือ table ที่แสดงโครงสร้างลำดับชั้นของ XML บางครั้ง elements ถูกแสดงเป็น record ที่ nested กัน คุณอาจต้องใช้ Table.ExpandRecordColumn เพื่อเปิดตัวข้อมูลออกมา

**Q: จะส่ง encoding ให้ Xml.Document ได้ยังไง?**

ใช้พารามิเตอร์ที่สองคือ encoding number เช่น 65001 (UTF-8), 1200 (UTF-16), 1252 (Windows-1252) เป็นต้น

**Q: Xml.Document กับ Json.Document ต่างกันตรงไหน?**

Xml.Document จัดการไฟล์ XML ส่วน Json.Document จัดการไฟล์ JSON ทั้งสองใช้โครงสร้างลำดับชั้นแต่รูปแบบไฟล์ต่างกัน

**Q: ถ้า XML parsing ผิดพลาดจะเกิดอะไรขึ้น?**

Power Query จะ throw error หากรูปแบบ XML ไม่ถูกต้องหรือ encoding ผิด อย่าลืมตรวจสอบ XML validity และ encoding ของไฟล์

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Xml.Document](https://learn.microsoft.com/en-us/powerquery-m/xml-document) _(official)_
- [Power Query M Function Reference - XML Functions](https://learn.microsoft.com/en-us/powerquery-m/xml-functions) _(official)_
- [Table.ExpandRecordColumn - Expand nested records](https://learn.microsoft.com/en-us/powerquery-m/table-expandrecordcolumn) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/accessing-data-functions/xml-document/](https://www.thepexcel.com/functions/power-query/accessing-data-functions/xml-document/)_
