LINQ-XML
LINQ to XML은 표준 쿼리 연산자, 프로그래밍 인터페이스 등과 같은 모든 LINQ 기능에 대한 쉬운 액세스를 제공합니다. .NET 프레임 워크에 통합 된 LINQ to XML은 디버깅, 컴파일 타임 검사, 강력한 입력과 같은 .NET 프레임 워크 기능을 최대한 활용합니다. 그리고 할 말이 더 많습니다.
LINQ to XML 소개
LINQ to XML을 사용하는 동안 XML 문서를 메모리로로드하는 것은 쉽고 쿼리 및 문서 수정이 더 쉽습니다. 메모리에있는 XML 문서를 디스크에 저장하고 직렬화 할 수도 있습니다. 개발자가 다소 복잡한 XML 쿼리 언어를 배울 필요가 없습니다.
LINQ to XML은 System.Xml.Linq 네임 스페이스에서 강력합니다. 여기에는 XML 작업에 필요한 19 개의 모든 클래스가 있습니다. 이 클래스는 다음과 같습니다.
- XAttribute
- XCData
- XComment
- XContainer
- XDeclaration
- XDocument
- XDocumentType
- XElement
- XName
- XNamespace
- XNode
- XNodeDocumentOrderComparer
- XNodeEqualityComparer
- XObject
- XObjectChange
- XObjectChangeEventArgs
- XObjectEventHandler
- XProcessingInstruction
- XText
LINQ를 사용하여 XML 파일 읽기
씨#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace LINQtoXML {
class ExampleOfXML {
static void Main(string[] args) {
string myXML = @"<Departments>
<Department>Account</Department>
<Department>Sales</Department>
<Department>Pre-Sales</Department>
<Department>Marketing</Department>
</Departments>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
var result = xdoc.Element("Departments").Descendants();
foreach (XElement item in result) {
Console.WriteLine("Department Name - " + item.Value);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
}
}
VB
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Module Module1
Sub Main(ByVal args As String())
Dim myXML As String = "<Departments>" & vbCr & vbLf &
"<Department>Account</Department>" & vbCr & vbLf &
"<Department>Sales</Department>" & vbCr & vbLf &
"<Department>Pre-Sales</Department>" & vbCr & vbLf &
"<Department>Marketing</Department>" & vbCr & vbLf &
"</Departments>"
Dim xdoc As New XDocument()
xdoc = XDocument.Parse(myXML)
Dim result = xdoc.Element("Departments").Descendants()
For Each item As XElement In result
Console.WriteLine("Department Name - " + item.Value)
Next
Console.WriteLine(vbLf & "Press any key to continue.")
Console.ReadKey()
End Sub
End Module
위의 C # 또는 VB 코드가 컴파일되고 실행되면 다음과 같은 결과가 생성됩니다.
Department Name - Account
Department Name - Sales
Department Name - Pre-Sales
Department Name - Marketing
Press any key to continue.
새 노드 추가
씨#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace LINQtoXML {
class ExampleOfXML {
static void Main(string[] args) {
string myXML = @"<Departments>
<Department>Account</Department>
<Department>Sales</Department>
<Department>Pre-Sales</Department>
<Department>Marketing</Department>
</Departments>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
//Add new Element
xdoc.Element("Departments").Add(new XElement("Department", "Finance"));
//Add new Element at First
xdoc.Element("Departments").AddFirst(new XElement("Department", "Support"));
var result = xdoc.Element("Departments").Descendants();
foreach (XElement item in result) {
Console.WriteLine("Department Name - " + item.Value);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
}
}
VB
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Module Module1
Sub Main(ByVal args As String())
Dim myXML As String = "<Departments>" & vbCr & vbLf &
"<Department>Account</Department>" & vbCr & vbLf &
"<Department>Sales</Department>" & vbCr & vbLf &
"<Department>Pre-Sales</Department>" & vbCr & vbLf &
"<Department>Marketing</Department>" & vbCr & vbLf &
"</Departments>"
Dim xdoc As New XDocument()
xdoc = XDocument.Parse(myXML)
xdoc.Element("Departments").Add(New XElement("Department", "Finance"))
xdoc.Element("Departments").AddFirst(New XElement("Department", "Support"))
Dim result = xdoc.Element("Departments").Descendants()
For Each item As XElement In result
Console.WriteLine("Department Name - " + item.Value)
Next
Console.WriteLine(vbLf & "Press any key to continue.")
Console.ReadKey()
End Sub
End Module
위의 C # 또는 VB 코드가 컴파일되고 실행되면 다음과 같은 결과가 생성됩니다.
Department Name - Support
Department Name - Account
Department Name - Sales
Department Name - Pre-Sales
Department Name - Marketing
Department Name - Finance
Press any key to continue.
특정 노드 삭제
씨#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace LINQtoXML {
class ExampleOfXML {
static void Main(string[] args) {
string myXML = @"<Departments>
<Department>Support</Department>
<Department>Account</Department>
<Department>Sales</Department>
<Department>Pre-Sales</Department>
<Department>Marketing</Department>
<Department>Finance</Department>
</Departments>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
//Remove Sales Department
xdoc.Descendants().Where(s =>s.Value == "Sales").Remove();
var result = xdoc.Element("Departments").Descendants();
foreach (XElement item in result) {
Console.WriteLine("Department Name - " + item.Value);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
}
}
VB
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Module Module1
Sub Main(args As String())
Dim myXML As String = "<Departments>" & vbCr & vbLf &
"<Department>Support</Department>" & vbCr & vbLf &
"<Department>Account</Department>" & vbCr & vbLf &
"<Department>Sales</Department>" & vbCr & vbLf &
"<Department>Pre-Sales</Department>" & vbCr & vbLf &
"<Department>Marketing</Department>" & vbCr & vbLf &
"<Department>Finance</Department>" & vbCr & vbLf &
"</Departments>"
Dim xdoc As New XDocument()
xdoc = XDocument.Parse(myXML)
xdoc.Descendants().Where(Function(s) s.Value = "Sales").Remove()
Dim result = xdoc.Element("Departments").Descendants()
For Each item As XElement In result
Console.WriteLine("Department Name - " + item.Value)
Next
Console.WriteLine(vbLf & "Press any key to continue.")
Console.ReadKey()
End Sub
End Module
위의 C # 또는 VB 코드가 컴파일되고 실행되면 다음과 같은 결과가 생성됩니다.
Department Name - Support
Department Name - Account
Department Name - Pre-Sales
Department Name - Marketing
Department Name - Finance
Press any key to continue.