Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
XML ağaçlarını sorgularken en yaygın sorunlardan biri, XML ağacının varsayılan ad alanına sahip olması durumunda geliştiricinin bazen sorguyu XML ad alanında değil gibi yazmasıdır.
Bu makaledeki ilk örnek kümesi, varsayılan ad alanında XML'nin yüklenmesinin ve ardından yanlış sorgulanmanın tipik bir yolunu gösterir.
İkinci örnek kümesi, ad alanında XML sorgulayabileceğiniz gerekli düzeltmeleri gösterir.
Daha fazla bilgi için bkz . Ad alanlarına genel bakış.
Örnek: Ad alanında XML üzerinde yanlış sorgu
Bu örnekte ad alanında XML oluşturma işlemi ve boş sonuç kümesi döndüren bir sorgu gösterilmektedir.
XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>");
IEnumerable<XElement> c1 =
from el in root.Elements("Child")
select el;
Console.WriteLine("Result set follows:");
foreach (XElement el in c1)
Console.WriteLine((int)el);
Console.WriteLine("End of result set");
Dim root As XElement = _
<Root xmlns='http://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>
Dim c1 As IEnumerable(Of XElement) = _
From el In root.<Child> _
Select el
Console.WriteLine("Result set follows:")
For Each el As XElement In c1
Console.WriteLine(el.Value)
Next
Console.WriteLine("End of result set")
Örnek şu sonucu verir:
Result set follows:
End of result set
Örnek: Ad alanında XML üzerinde düzgün bir sorgu
Bu örnekte ad alanında XML oluşturma işlemi ve düzgün kodlanmış bir sorgu gösterilmektedir.
Çözüm, bir XNamespace nesneyi bildirmek ve başlatmak ve nesneleri belirtirken XName kullanmaktır. Bu durumda, yönteminin Elements bağımsız değişkeni bir XName nesnedir.
XElement root = XElement.Parse(
@"<Root xmlns='http://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>");
XNamespace aw = "http://www.adventure-works.com";
IEnumerable<XElement> c1 =
from el in root.Elements(aw + "Child")
select el;
Console.WriteLine("Result set follows:");
foreach (XElement el in c1)
Console.WriteLine((int)el);
Console.WriteLine("End of result set");
Imports <xmlns="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim root As XElement = _
<Root xmlns='http://www.adventure-works.com'>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<AnotherChild>4</AnotherChild>
<AnotherChild>5</AnotherChild>
<AnotherChild>6</AnotherChild>
</Root>
Dim c1 As IEnumerable(Of XElement) = _
From el In root.<Child> _
Select el
Console.WriteLine("Result set follows:")
For Each el As XElement In c1
Console.WriteLine(CInt(el))
Next
Console.WriteLine("End of result set")
End Sub
End Module
Örnek şu sonucu verir:
Result set follows:
1
2
3
End of result set