I use C# so bear with me:
You loaded the document correctly, but you got a null reference because your
statement is wrong. I did the following, and it worked.
Here is my simple XML document (im guessing yours is close to this)
<root>
<customers>
<topic year="1963">JFK Assassinated - November 22</topic>
<topic year="1980">John Lennon Assassinated - December 8</topic>
</customers>
</root>
And this is what I did.
//First, I opened my XML file.
XDocument doc = XDocument.Load("
http://localhost:60857/WebSite1/XMLFile.xml");
//Then I made an anonymous variable
var q = from c in
doc.Elements("root").Elements("customers").Elements("topic")
//The from statement selects text from the "topic" XName
where c.Attribute("year").Value == "1980"
//The where statement filters records by an attribute named
year.
select c;
//select statement selected only topics with the year
attribute set to 1980
foreach (string name in q)
{
Label1.Text = name;
//This line wrote "John Lennon Assassinated - December 8"
correctly. Change it to 1963, and see what happens.
}
On Mon, Dec 15, 2008 at 2:24 PM, Brock <[email protected]> wrote:
>
> I looked on MSDN for LINQ which I'm new to... used this code as an
> event for a button, but the compiler didn't like any of this:
>
> Public Sub XLinq25()
> Dim doc = XDocument.Load(Server.MapPath("history.xml"))
> Dim query = _
> From c In doc.<Root>.<Customers> _
> Where c.<Topic>.<Year>.Value = "1963" _
> Select c
>
> For Each result In query
> Console.WriteLine(result)
> Next
> End Sub
>
> On Dec 15, 12:12 pm, "Brandon Betances" <[email protected]> wrote:
> > LINQ to XML, get the record ID you want, pass it as a query string
> >
> >
> >
> > On Mon, Dec 15, 2008 at 12:08 PM, Brock <[email protected]> wrote:
> >
> > > I have an .aspx page that is successfully (I use Visual Web Developer
> > > 2008) reading an XML file and displaying the records on a datagrid
> > > using:
> >
> > > <script runat="server">
> > > Private Function MakeDataView() as DataView
> > > Dim myDataSet As New DataSet()
> > > myDataSet.ReadXml(Server.MapPath("history.xml"))
> > > Dim view As DataView = New DataView(myDataSet.Tables(0))
> > > view.AllowDelete = False
> > > view.AllowEdit = False
> > > view.AllowNew = False
> > > Return view
> > > End Function
> >
> > > Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
> > > Dim view as DataView = MakeDataView()
> > > dgHistory.DataSource = view
> > > dgHistory.AllowSorting = True
> > > dgHistory.DataBind()
> > > Label1.Text = DateAndTime.Now.ToLongTimeString
> > > End Sub
> > > </script>
> >
> > > Can anyone give me some clues as to how to code another .aspx page to
> > > query the XML using XPathNavigator (or something else) tools to find a
> > > specific node
> > > based on an ID number? The goal is to have the user be able to click
> > > on an edit button and it will launch an edit page passing that record
> > > to the edit page.
> >
> > > I really need a separate edit page because the number of fields is
> > > over 50 and the datagrid in the base form only displays about 4
> > > fields.- Hide quoted text -
> >
> > - Show quoted text -