Click here to Skip to main content
15,569,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
            -<UTILITYTYPE CODE="1">
            <G2>27-05-2021 01:25:49</G2>

Here I want to read value of <g1> tag i.e. 04220040 from many files.


What I have tried:

XmlDocument infodoc = new XmlDocument();
            IEnumerable<XElement> direclty = infodoc.GetElementsByTagName("Settings").Elements("directory");
            var rosterUserIds = direclty.Select(r => r.Value);

Can you please suggest what error am i making or a better way to get the value of <g1> tag.
Updated 24-Jun-21 1:38am
Maciej Los 24-Jun-21 6:32am    
An attribute 'value' does not exist!
var rosterUserIds = direclty.Select(r => r.Value).SingleOrDefault()
Himansh jain 24-Jun-21 6:39am    
still it is not working...what to do?

1 solution

Another way is to use XDocument[^]:

XDocument xdoc = XDocument.Load("full_file_name_here");
var uid = xdoc.Descendants("G1").First()?.Value;
Share this answer
Himansh jain 24-Jun-21 6:53am    
thank you for your solution . Could you please tell where I can study about "Descendants("G1").First()?.Value" usage...i was going through documentation but could not find it .
Maciej Los 24-Jun-21 6:56am    
This part of code, which uses "First" is called Linq to xml. See: Overview - LINQ to XML | Microsoft Docs[^]
Himansh jain 24-Jun-21 7:42am    
thank you:)
Maciej Los 24-Jun-21 7:44am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900