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:
<CDF>
            -<UTILITYTYPE CODE="1">
            -<D1><G1>04220040</G1>
            <G2>27-05-2021 01:25:49</G2>
            </D1></UTILITYTYPE></CDF>


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

Output
04220040
.

What I have tried:

XmlDocument infodoc = new XmlDocument();
            infodoc.Load("C:\\Users\\himansh.jain\\Downloads\\cesu\\001_04220040_20210527_03162969.xml");
            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.
Posted
Updated 24-Jun-21 1:38am
v2
Comments
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[^]:

C#
XDocument xdoc = XDocument.Load("full_file_name_here");
var uid = xdoc.Descendants("G1").First()?.Value;
 
Share this answer
 
Comments
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