I am having a lot of trouble translating examples I am finding into use with XML.
Starting with this (a very cut down version of the whole XML file)
<?xml version="1.0" encoding="UTF-8"?>
<MetadataSets>
<MetadataSet MetadataSetID="999_never" >
<MetadataSetItem>
<TagName>NeverSeen</TagName>
<ItemSeq>10</ItemSeq>
</MetadataSetItem>
<MetadataSetItem>
<TagName>AlsoNeverSeen</TagName>
<ItemSeq>20</ItemSeq>
</MetadataSetItem>
</MetadataSet>
</MetadataSets>
What I am trying to do is create an ordered list of 2 fields which, eventually, I hope to return as a List of Tuples
'NeverSeen' 10
'AlsoNeverSeen' 20
The following are a list of several approaches I have tried – mostly included to show I have made an effort before calling for help.
So I would love some help to put me on the right trail for this specific problem but also does anybody have a suggestion for a decent, non trivial, tutorial on LINQ for XML.
I should say I am coming from a Database background; 30 seconds in SQL – 5 days and counting for XML and LINQ.
I am also getting confused with different approaches – and probably inadvertantly mixing them
eg those that use ‘from’
var schema3 = from el in XDoc.Elements("MetadataDomain")
where (String)el.Attribute("DomainUUID") == pDomainUUID
select (el) ;
then iterate the ‘el’ even if there is only one.
or those that dont use ‘from’ but use ‘.’
var schema4 = XDoc.Elements("MetadataDomain")
.Where (sch => (String)sch.Attribute("DomainUUID") == pDomainUUID)
.Select (sch => (String)sch.Element("SchemaName").Value );
then you still have to iterate the result even if there is only one.
I was warming to XPath eg
string schema5 = root.XPathSelectElement($"MetadataSets/MetadataDomain[@DomainUUID='{pDomainUUID}']").Element("SchemaName").Value;
but went off that because its all on one line so when it crashes there is no way to debug anything. And pretty much anything you type in will compile.
And how do I select 2 ‘fields’, for example some say to select 2 ‘fields’ use anonymous types eg
select new { car.Make, car.VIN}
when I try that with XML
where (String)el.Attribute("MetadataSetID") == pMetadataSetID
select new { el.Element("TagName").Value, el.Element("ItemSeq").Value } ;
it gives compile error error CS0833: An anonymous type cannot have multiple properties with the same name
seems the compiler cant tell these apart and I dont know how to alias them.
Thanks
JC