New LinqPad user help.
I have started looking into LinqPad to help me with my LINQ querys in vb.net programs. What I would like to do is export my xml dataset then use that in LinqPad. I have exported my dataset but I have been having trouble setting the XML as a datasource and running a simple query against it. When I use the code in the FAQ I get an error saying Object reference not set to an instance of an object. It is probably since I have multiply tables in this xml and I am not sure how to specify the table. I am attaching a screen capture of the XML datasource with 4 tables in it and I have expanded one table showing the columns. Any help to get me started would be great. Here is the code I have:
var xml = XElement.Load (@c:\\TEMP\MyData.xml);
var query =
from e in xml.Elements()
where e.Attribute ("LocNum").Value == "Something"
select e;
query.Dump();
Please remember I would like to work in VB.net.
A valid table name is "tbl_MsView_Items"
Thank you for all the help.
var xml = XElement.Load (@c:\\TEMP\MyData.xml);
var query =
from e in xml.Elements()
where e.Attribute ("LocNum").Value == "Something"
select e;
query.Dump();
Please remember I would like to work in VB.net.
A valid table name is "tbl_MsView_Items"
Thank you for all the help.
Comments
However the code works.
When supplying the correct input.
C#: Input: Output: When supplying incorrect input: .., the code fails (note that the incorrect line is highlighted) on the following line: Because the attribute LocNum is absent.
You can fix this by replacing the above line with these: This will check for an existing attribute before checking its value.
Or the LINQPad 5 method:
If you'd like to code in VB.Net then you should supply an example in VB.Net Or run the code through an converter: http://converter.telerik.com/
Thank you. I am a total newb to this. The only reason I was using the C# statements was that was all that was in the FAQ. I did run it through the converter to this. Thank you for that link to the converter.
Dim xml = XElement.Load("c:\\TEMP\MyData.xml")
Dim query = From e In xml.Elements() Where e.Attribute("LocNum").Value = "Someplace"
query.Dump()
Since the screen shot of the data did not come through. I will paste the some raw data instead. This XML has 4 tables in it, below is just one.
Snippet of the xml Schema
....
Snippet of the data
Cust1
Someplace
Somegroup
1030928
abcdef
60.01
Once again thank you for your patience. I am really looking to just get started with a simple query to pull back records.
(Also, the data you posted is missing the necessary xml markup)
Edit: It's great to start with "a simple query" (we've all started with simple examples), but you have to understand what the error means and how to solve it.
The error is: "Object reference not set to an instance of an object." and the cursor points to the equation:
where e.Attribute("LocNum").Value == "Something"
. This means that somewhere in this equation, a reference to an object is missing. Most likely, there is a tag without an attribute LocNum for which no propertyValue
can be read.I have given a solution for this, so it's up to you to give it a try and show what happens next.
Once again thank you.
Yes I did try the fix and it works and nothing is returned, no errors I think that the issue is referencing the correct tags in the XML. I do not know the correct term for this in pertaining to XML, but the XML has 4 tables. In the past in linq I reference the table like this in vb.net
Dim query = From H In dataset.Tables("SomeTable").AsEnumerable() _ Where H.Field(Of String)("SomeColumn").Equals (SomeString) _
I am not sure how to do this referencing the table then the column in XML. I know the statement is not returning anything since the "LocNum" is actually a sub-child element.
I was hoping to just post a little snippet of the XML but the actual XML did not make it.
Here is the XML and one data row
<?xml version="1.0" standalone="yes"?> <ProgramData> <xs:schema id="ProgramData" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="ProgramData" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="tbl_OpenOrdersSummary"> <xs:complexType> <xs:sequence> <xs:element name="ord_no" type="xs:string" minOccurs="0" /> <xs:element name="cus_no" type="xs:string" minOccurs="0" /> <xs:element name="cus_alt_adr_cd" type="xs:string" minOccurs="0" /> <xs:element name="ord_type" type="xs:string" minOccurs="0" /> <xs:element name="Ord_Type_Desc" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="tbl_MsView_Items"> <xs:complexType> <xs:sequence> <xs:element name="CustNum" type="xs:string" minOccurs="0" /> <xs:element name="LocNum" type="xs:string" minOccurs="0" /> <xs:element name="CustGroup" type="xs:string" minOccurs="0" /> <xs:element name="ItemCode" type="xs:string" minOccurs="0" /> <xs:element name="ItemDesc" type="xs:string" minOccurs="0" /> <xs:element name="ItemOrder" type="xs:string" minOccurs="0" /> <xs:element name="CustPartNum" type="xs:string" minOccurs="0" /> <xs:element name="DirectShip" type="xs:string" minOccurs="0" /> <xs:element name="Consign" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <tbl_MsView_Items> <CustNum>SomeCust</CustNum> <LocNum>SomeLoc</LocNum> <CustGroup>SomeGroup</CustGroup> <ItemCode>12198604</ItemCode> <ItemDesc>Description</ItemDesc> <ItemOrder>60.01</ItemOrder> <CustPartNum>abc123</CustPartNum> <DirectShip>No</DirectShip> <Consign>No</Consign> </tbl_MsView_Items> </ProgramData>
DataSet:
Otherwise, LINQ to XML:
Thank you. Now that works. I guess I did not know I could do it as a Dataset, My thinking was that I did not have access to the dataset to test like this. I was just thinking XML. I have worked so little LINQ I was hoping to use this tool to write and test LINQ queries outside of Visual Studio. By using XML I can get a snapshot of what my data looks like at that point in the program. If I can design it in Dataset format it makes it easier to slap back in the program.
This gives me a good place to start. I was really hoping this would work. I have no problem buying the full product but I just wanted to at least see if I could use it. I looks like we can definately use it.
I will continue to work with this some more, Seeing as I can work directly like it is a dataset, it will make life easier.
Thank you for your help.