마크는 옳습니다. VB는 좋은 물건을 많이 만듭니다. 나는 C#을 사용하는 사람이지만, VB 솔루션을 사용하여 어떻게 처리하는지 살펴 보았습니다. 아래 코드를 게시하고 핵심 부분을 설명했습니다. 나는 VB가 XML을 가지고있는 기능에 매우 감동했다!
코드 샘플에서 Xml을 XDocument로로드 할 수있었습니다. XDocument.Load를 완료하면 몇 가지 특수 구문을 사용하여 XML 문서에 액세스 할 수 있습니다.
처음에는 문서에서 모든 제품을 가져오고 싶습니다. 즉 모두 < 제품> 요소입니다. 우리는 다음을 수행해야합니다
Dim products = productsDoc...<Product>
이 문서의 모든 < 제품 소개> 요소를 원하는 말한다. 이것은 우리에게 XElements의 IEnumerable 컬렉션을 제공합니다.
컬렉션에서 개별 제품을 가져온 후에는 이름이나 가격과 같은 제품의 값에 액세스하고자합니다. 당신이 볼 수 있도록
이
' this gets the value of the price element within a product
product.<Price>.Value
여기에 예상되는 출력과 함께 전체 예제 : 우리는 다음을 수행 할 필요가 있음을하려면
Module Module1
' some products xml to use for this example
Dim productsXml = <Xml>
<Product>
<Name>Mountain Bike</Name>
<Price>59.99</Price>
</Product>
<Product>
<Name>Arsenal Football</Name>
<Price>9.99</Price>
</Product>
<Product>
<Name>Formula One Cap</Name>
<Price>14.99</Price>
</Product>
<Product>
<Name>Robin Hood Bow</Name>
<Price>8.99</Price>
</Product>
</Xml>
Sub Main()
' load the xml into an XDocument
' NOTE: this line isn't needed when using inline XML as per this example,
' but I wanted to make this code easy to modify for reading in text files
Dim productsDoc = System.Xml.Linq.XDocument.Parse(productsXml.ToString())
' get all <Product> elements from the XDocument
Dim products = From product In productsDoc...<Product> _
Select product
' go through each product
For Each product In products
' output the value of the <Name> element within product
Console.WriteLine("Product name is {0}", product.<Name>.Value)
' output the value of the <Price> element within product
Console.WriteLine("Product price is {0}", product.<Price>.Value)
Next
End Sub
End Module
프로그램의 출력은 다음과 같습니다
Product name is Mountain Bike
Product price is 59.99
Product name is Arsenal Football
Product price is 9.99
Product name is Formula One Cap
Product price is 14.99
Product name is Robin Hood Bow
Product price is 8.99
이것이 도움이되기를 바랍니다. 더 자세한 정보를 원하면 :-)
취침 시간에 일관된 것을 쓰는 것이 어렵습니다. :-)
실제로 LINQ의 동작은 C#과 VB에서 비슷합니다. xml은 차이가있는 몇 가지 영역 중 하나입니다. VB에서 물건을 추가하는 것이지 C#이 아닙니다. -p –
ok ...이 문제를 해결하는 데 도움이되는 방법은 무엇입니까? – Jason
Json은 xml 도움을 찾고 ... 그게 좀 재밌 네요! –