Linq to XML - Query returns nothing
I have a XML file on which when I run a LINQ query returns nothing in the for loop.
<?xml version="1.0" encoding="utf-8"?>
<ReadOrderResponseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Status xmlns="urn:mysite:apis">Success</Status>
<TimeStamp xmlns="urn:mysite:apis">2010-07-25T15:26:49.1150235Z</TimeStamp>
<StoreUrl xmlns="urn:mysite:apis">http://XYZ.COM</StoreUrl>
<PageResponse xmlns="urn:mysite:apis">
<Page>1</Page>
<Size>10</Size>
<TotalSize>18033</TotalSize>
<HasMore>true</HasMore>
</PageResponse>开发者_如何转开发;
<OrderList OrderId="20147184" OrderNumber="20100560" xmlns="urn:mysite:apis">
<Archived>false</Archived>
<CreateDate>2010-07-25T01:37:04.067Z</CreateDate>
</OrderList>
</ReadOrderResponseType>
VB.NET:
Dim document As XDocument = XDocument.Parse(myOrders.xml)
Dim query = From O In document.Root.Elements("OrderList") _
Select New With _
{ _
.Archived= O.Element("Archived").Value, _
.Status = O.Element("CreateDate").Value _
}
For Each Order In query
Console.WriteLine(Order.Archived)
Console.ReadLine()
Next
It does not even go inside the for loop. What am I doing wrong?
You need a namespace qualifier. Try something like:
Dim document As XDocument = XDocument.Load("XMLFile1.xml")
Dim apins As XNamespace = "urn:mysite:apis"
Dim query = From O In document.Root.Elements(apins + "OrderList") _
Select New With _
{ _
.Archived = O.Element(apins + "Archived").Value, _
.Status = O.Element(apins + "CreateDate").Value _
}
For Each Order In query
Console.WriteLine(Order.Archived)
Console.ReadLine()
Next
精彩评论