LINQ to XML 축 메서드를 작성하는 방법(LINQ to XML)
XML 축 메서드는 XML 문서 또는 상위 요소에서 XML 요소 컬렉션을 검색합니다. XML 트리에서 컬렉션을 검색하는 축 메서드를 작성할 수 있습니다. 축 메서드를 작성하는 가장 좋은 방법 중 하나는 요소나 특성의 컬렉션을 반환하는 확장 메서드를 작성하는 것입니다. 애플리케이션의 요구 사항에 따라 요소나 특성의 특정 하위 집합을 반환하는 확장명 메서드를 작성할 수 있습니다. 이 문서에서는 C# 및 Visual Basic에 대한 예를 제공합니다.
예시
다음 예제에서는 두 가지 확장명 메서드를 사용합니다. 첫 번째 확장 메서드인 GetXPath
는 XObject에 대해 작동하며 계산될 때 노드나 특성을 반환할 XPath 식을 반환합니다. 두 번째 확장 메서드인 Find
는 XElement에 대해 작동하며 지정된 일부 텍스트가 포함된 XAttribute 개체와 XElement 개체의 컬렉션을 반환합니다.
이 예에서는 XML 문서 샘플 XML 파일: 여러 구매 주문서를 사용합니다.
public static class MyExtensions
{
private static string GetQName(XElement xe)
{
string prefix = xe.GetPrefixOfNamespace(xe.Name.Namespace);
if (xe.Name.Namespace == XNamespace.None || prefix == null)
return xe.Name.LocalName.ToString();
else
return prefix + ":" + xe.Name.LocalName.ToString();
}
private static string GetQName(XAttribute xa)
{
string prefix =
xa.Parent.GetPrefixOfNamespace(xa.Name.Namespace);
if (xa.Name.Namespace == XNamespace.None || prefix == null)
return xa.Name.ToString();
else
return prefix + ":" + xa.Name.LocalName;
}
private static string NameWithPredicate(XElement el)
{
if (el.Parent != null && el.Parent.Elements(el.Name).Count() != 1)
return GetQName(el) + "[" +
(el.ElementsBeforeSelf(el.Name).Count() + 1) + "]";
else
return GetQName(el);
}
public static string StrCat<T>(this IEnumerable<T> source,
string separator)
{
return source.Aggregate(new StringBuilder(),
(sb, i) => sb
.Append(i.ToString())
.Append(separator),
s => s.ToString());
}
public static string GetXPath(this XObject xobj)
{
if (xobj.Parent == null)
{
XDocument doc = xobj as XDocument;
if (doc != null)
return ".";
XElement el = xobj as XElement;
if (el != null)
return "/" + NameWithPredicate(el);
// The XPath data model doesn't include white space text nodes
// that are children of a document, so this method returns null.
XText xt = xobj as XText;
if (xt != null)
return null;
XComment com = xobj as XComment;
if (com != null)
return
"/" +
(
com
.Document
.Nodes()
.OfType<XComment>()
.Count() != 1 ?
"comment()[" +
(com
.NodesBeforeSelf()
.OfType<XComment>()
.Count() + 1) +
"]" :
"comment()"
);
XProcessingInstruction pi = xobj as XProcessingInstruction;
if (pi != null)
return
"/" +
(
pi.Document.Nodes()
.OfType<XProcessingInstruction>()
.Count() != 1 ?
"processing-instruction()[" +
(pi
.NodesBeforeSelf()
.OfType<XProcessingInstruction>()
.Count() + 1) +
"]" :
"processing-instruction()"
);
return null;
}
else
{
XElement el = xobj as XElement;
if (el != null)
{
return
"/" +
el
.Ancestors()
.InDocumentOrder()
.Select(e => NameWithPredicate(e))
.StrCat("/") +
NameWithPredicate(el);
}
XAttribute at = xobj as XAttribute;
if (at != null)
return
"/" +
at
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Select(e => NameWithPredicate(e))
.StrCat("/") +
"@" + GetQName(at);
XComment com = xobj as XComment;
if (com != null)
return
"/" +
com
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Select(e => NameWithPredicate(e))
.StrCat("/") +
(
com
.Parent
.Nodes()
.OfType<XComment>()
.Count() != 1 ?
"comment()[" +
(com
.NodesBeforeSelf()
.OfType<XComment>()
.Count() + 1) + "]" :
"comment()"
);
XCData cd = xobj as XCData;
if (cd != null)
return
"/" +
cd
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Select(e => NameWithPredicate(e))
.StrCat("/") +
(
cd
.Parent
.Nodes()
.OfType<XText>()
.Count() != 1 ?
"text()[" +
(cd
.NodesBeforeSelf()
.OfType<XText>()
.Count() + 1) + "]" :
"text()"
);
XText tx = xobj as XText;
if (tx != null)
return
"/" +
tx
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Select(e => NameWithPredicate(e))
.StrCat("/") +
(
tx
.Parent
.Nodes()
.OfType<XText>()
.Count() != 1 ?
"text()[" +
(tx
.NodesBeforeSelf()
.OfType<XText>()
.Count() + 1) + "]" :
"text()"
);
XProcessingInstruction pi = xobj as XProcessingInstruction;
if (pi != null)
return
"/" +
pi
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Select(e => NameWithPredicate(e))
.StrCat("/") +
(
pi
.Parent
.Nodes()
.OfType<XProcessingInstruction>()
.Count() != 1 ?
"processing-instruction()[" +
(pi
.NodesBeforeSelf()
.OfType<XProcessingInstruction>()
.Count() + 1) + "]" :
"processing-instruction()"
);
return null;
}
}
public static IEnumerable<XObject> Find(this XElement source, string value)
{
if (source.Attributes().Any())
{
foreach (XAttribute att in source.Attributes())
{
string contents = (string)att;
if (contents.Contains(value))
yield return att;
}
}
if (source.Elements().Any())
{
foreach (XElement child in source.Elements())
foreach (XObject s in child.Find(value))
yield return s;
}
else
{
string contents = (string)source;
if (contents.Contains(value))
yield return source;
}
}
}
class Program
{
static void Main(string[] args)
{
XElement purchaseOrders = XElement.Load("PurchaseOrders.xml");
IEnumerable<XObject> subset =
from xobj in purchaseOrders.Find("1999")
select xobj;
foreach (XObject obj in subset)
{
Console.WriteLine(obj.GetXPath());
if (obj.GetType() == typeof(XElement))
Console.WriteLine(((XElement)obj).Value);
else if (obj.GetType() == typeof(XAttribute))
Console.WriteLine(((XAttribute)obj).Value);
}
}
}
Imports System.Runtime.CompilerServices
Imports System.Text
Module Module1
Sub Main()
Dim purchaseOrders = XElement.Load("..\..\PurchaseOrders.xml")
Dim subset = From xobj In purchaseOrders.Find("1999")
For Each obj In subset
Console.WriteLine(obj.GetXPath())
If obj.GetType() = GetType(XElement) Then
Console.WriteLine(CType(obj, XElement).Value)
ElseIf obj.GetType() = GetType(XAttribute) Then
Console.WriteLine(CType(obj, XAttribute).Value)
End If
Next
End Sub
End Module
Public Module MyExtensions
Private Function GetQName(ByVal xe As XElement) As String
Dim prefix = xe.GetPrefixOfNamespace(xe.Name.Namespace)
If xe.Name.Namespace = XNamespace.None OrElse prefix Is Nothing Then
Return xe.Name.LocalName
Else
Return prefix & ":" & xe.Name.LocalName
End If
End Function
Private Function GetQName(ByVal xa As XAttribute) As String
Dim prefix = xa.Parent.GetPrefixOfNamespace(xa.Name.Namespace)
If xa.Name.Namespace = XNamespace.None OrElse prefix Is Nothing Then
Return xa.Name.LocalName
Else
Return prefix & ":" & xa.Name.LocalName
End If
End Function
Private Function NameWithPredicate(ByVal el As XElement) As String
If el.Parent IsNot Nothing AndAlso
el.Parent.Elements(el.Name).Count() <> 1 Then
Return GetQName(el) & "[" &
(el.ElementsBeforeSelf(el.Name).Count() + 1) & "]"
Else
Return GetQName(el)
End If
End Function
<Extension()>
Public Function StrCat(Of T)(ByVal source As IEnumerable(Of T),
ByVal separator As String) As String
Return source.Aggregate(New StringBuilder,
Function(sb, i) sb.
Append(i.ToString()).
Append(separator),
Function(s) s.ToString())
End Function
<Extension()>
Public Function GetXPath(ByVal xobj As XObject) As String
If xobj.Parent Is Nothing Then
Dim doc = TryCast(xobj, XDocument)
If doc IsNot Nothing Then Return "."
Dim el = TryCast(xobj, XElement)
If el IsNot Nothing Then Return "/" + NameWithPredicate(el)
' The XPath data model doesn't include white space text nodes
' that are children of a document, so this method returns null.
Dim xt = TryCast(xobj, XText)
If xt IsNot Nothing Then Return Nothing
Dim com = TryCast(xobj, XComment)
If com IsNot Nothing Then
Return "/" &
If(com.Document.Nodes().OfType(Of XComment)().Count() <> 1,
"comment()[" & (com.NodesBeforeSelf().OfType(Of XComment)().Count() + 1) & "]",
"comment()")
End If
Dim pi = TryCast(xobj, XProcessingInstruction)
If pi IsNot Nothing Then
Return "/" &
If(pi.Document.Nodes().OfType(Of XProcessingInstruction)().Count() <> 1,
"processing-instruction()[" &
(pi.NodesBeforeSelf().OfType(Of XProcessingInstruction)().Count() + 1) & "]",
"processing-instruction()")
End If
Return Nothing
Else
Dim el = TryCast(xobj, XElement)
If el IsNot Nothing Then
Return "/" &
el.Ancestors().
InDocumentOrder().
Select(Function(e) NameWithPredicate(e)).StrCat("/") & NameWithPredicate(el)
End If
Dim at = TryCast(xobj, XAttribute)
If at IsNot Nothing Then
Return "/" &
at.Parent.
AncestorsAndSelf().
InDocumentOrder().
Select(Function(e) NameWithPredicate(e)).StrCat("/") & "@" & GetQName(at)
End If
Dim com = TryCast(xobj, XComment)
If com IsNot Nothing Then
Return "/" &
com.Parent.
AncestorsAndSelf().
InDocumentOrder().
Select(Function(e) NameWithPredicate(e)).StrCat("/") &
If(com.Parent.Nodes().OfType(Of XComment)().Count() <> 1,
"comment()[" & (com.NodesBeforeSelf().OfType(Of XComment)().Count() + 1) & "]",
"comment()")
End If
Dim cd = TryCast(xobj, XCData)
If cd IsNot Nothing Then
Return "/" &
cd.Parent.
AncestorsAndSelf().
InDocumentOrder().
Select(Function(e) NameWithPredicate(e)).StrCat("/") &
If(cd.Parent.Nodes().OfType(Of XText)().Count() <> 1,
"text()[" & (cd.NodesBeforeSelf().OfType(Of XText)().Count() + 1) & "]",
"text()")
End If
Dim tx = TryCast(xobj, XText)
If tx IsNot Nothing Then
Return "/" &
tx.Parent.
AncestorsAndSelf().
InDocumentOrder().
Select(Function(e) NameWithPredicate(e)).StrCat("/") &
If(tx.Parent.Nodes().OfType(Of XText)().Count() <> 1,
"text()[" & (tx.NodesBeforeSelf().OfType(Of XText)().Count() + 1) & "]",
"text()")
End If
Dim pi As XProcessingInstruction = TryCast(xobj, XProcessingInstruction)
If pi IsNot Nothing Then
Return "/" &
pi.Parent.
AncestorsAndSelf().
InDocumentOrder().
Select(Function(e) NameWithPredicate(e)).StrCat("/") &
If(pi.Parent.Nodes().OfType(Of XProcessingInstruction)().Count() <> 1,
"processing-instruction()[" &
(pi.NodesBeforeSelf().OfType(Of XProcessingInstruction)().Count() + 1) & "]",
"processing-instruction()")
End If
Return Nothing
End If
End Function
<Extension()>
Public Function Find(ByVal source As XElement, ByVal value As String) As IEnumerable(Of XObject)
Dim results = From att In source.Attributes()
Where att.Value.Contains(value)
Let a As XObject = att
Select a
If source.Elements().Any Then
For Each result In From child In source.Elements() Select Find(child, value)
results = If(results Is Nothing, result, results.Union(result))
Next
Else
If source.Value.Contains(value) Then
results = If(results Is Nothing,
New List(Of XObject) From {source},
results.Union(New List(Of XObject) From {source}))
End If
End If
Return results
End Function
End Module
이 예제는 다음과 같은 출력을 생성합니다.
/PurchaseOrders/PurchaseOrder[1]/@OrderDate
1999-10-20
/PurchaseOrders/PurchaseOrder[1]/Items/Item[2]/ShipDate
1999-05-21
/PurchaseOrders/PurchaseOrder[2]/@OrderDate
1999-10-22
/PurchaseOrders/PurchaseOrder[3]/@OrderDate
1999-10-22
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET