Reputation: 99
I have a XDocument something like this:
<LookupList>
<ImageInfo1>
<FieldID>1057</FieldID>
<FieldName>Lookup_test</FieldName>
<LookupText>open</LookupText>
</ImageInfo1>
<ImageInfo1>
<FieldID>1057</FieldID>
<FieldName>Lookup_test</FieldName>
<LookupText>Waiting for input</LookupText>
</ImageInfo1>
<ImageInfo1>
<FieldID>1057</FieldID>
<FieldName>Lookup_NEW_NAME</FieldName>
<LookupText>Closed</LookupText>
</ImageInfo1>
</LookupList>
I want to get
Can someone give me a hint how to go about it?
Upvotes: 2
Views: 2416
Reputation: 2320
First you need to know how to read the elements from your XDocument:
public void ReadXDocument(XDocument doc)
{
foreach (XElement el in doc.Descendants("ImageInfo1"))
{
string fieldid = el.Element("FieldID").Value;
string fieldName = el.Element("FieldName").Value;
string lookupText = el.Element("LookupText").Value;
}
Once you know that, using Linq to achieve your goal is relatively straight forward.
This should give you a list of distinct FieldNames:
List<String> distinctNames = doc.Descendants("ImageInfo1")
.Select(o => o.Element("FieldName").Value)
.Distinct().ToList();
And this should give you a collection of LookupText values for each FieldName
IEnumerable groups = doc.Descendants("ImageInfo1")
.GroupBy(o => o.Element("FieldName").Value)
.Select(o => new { Key = o.Key, Lookups = o.ToList() });
}
Upvotes: 3