8
Với XML này ...Tại sao không phải là LINQ to XML Query làm việc (Amazon S3)
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>public.rpmware.com</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>0.dir</Key>
<LastModified>2008-06-25T16:09:49.000Z</LastModified>
<ETag>"0ba2a466f9dfe225d7ae85277a99a976"</ETag>
<Size>16</Size>
<Owner>
<ID>1234</ID>
<DisplayName>kyle</DisplayName>
</Owner>
<StorageClass>STANDARD</StorageClass>
</Contents>
<!-- repeat similar 100x -->
</ListBucketResult>
Và C# code này:
XDocument doc = XDocument.Load(xmlReader);
var contents = from content in doc.Descendants("Contents") select new {Key = content.Element("Key").Value, ETag = content.Element("ETag").Value};
foreach (var content in contents)
{
Console.WriteLine(content.Key);
Console.WriteLine(content.ETag);
}
Tôi biết Xdoc là không có sản phẩm nào và chứa XML đúng.
Tôi cũng đã triển khai một số mã ScottGu (http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx) làm kiểm tra độ chính xác và hoạt động chính xác như mong đợi.
XDocument doc2 = XDocument.Load(@"http://weblogs.asp.net/scottgu/rss.aspx");
var posts = from items in doc2.Descendants("item") select new { Title = items.Element("title").Value };
foreach (var post in posts)
{
Console.WriteLine(post.Title);
}
cảm ơn bạn, cảm ơn bạn, cảm ơn bạn. Tôi đã đập đầu vào tường trong khoảng 2 tiếng đồng hồ. ước gì tôi có thể bầu nhiều hơn một lần. –
Tôi cũng vậy - Tôi không biết về doc.Descendants và luôn sử dụng doc.Elements()! – Perhentian