Can somebody explain why is this xml
<?xml version="1.0" encoding="utf-8"?>
<items>
<item id="77" cityID="EE12345" cityDatum="15.2.2010. 11:28:35" />
</items>
when using query
Dim c = From items In st.Descendants _
Where items.@id IsNot Nothing _
Select New myStorage With {.id = items.@id, .cityID = items.@cityID, .cityDatum = items.@cityDatum}
storage = c.ToList
resulting in list(of myStorage) with two items – one with all empty (nothing) properties, second with values seen in xml above?
I’ve resolved the issue by adding
Where items.@id IsNot Nothing _
before Seletct New myStorage, but I have a feeling that I shouldn’t be doing that.
I’ve recreated this in C#, storage.xml is exactly the same as specified above.
private void Form1_Load(object sender, EventArgs e)
{
XDocument st;
st = XDocument.Load("C:\\storage.xml");
Object c = from items in st.Descendants()
select new {id = items.Attribute("id"), cityID = items.Attribute("cityID"), cityDatum = items.Attribute("cityDatum")};
}
If you, as some can’t replicate these results, here’s a screenshot:
Got it from your updated code sample. When you say
and then
your
stis the document, not the root element. So when you ask it for itsDescendants, you enumerate over 2XElements – theitemsnode and the oneitemnode that this example have. It is theitemsnode that is giving you the ’empty’ item.There are numerous ways to restrict yourself to just the
itemnodes, depending on how sure you can be about the structure of your xml – for example,will enumerate over just the
itemnodes found as immediate children of the document’s root element.