Click here to Skip to main content
15,069,133 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
<?xml version="1.0"?><CDF>
<UTILITYTYPE CODE="1">
<D2>
<INSTPARAM CODE="P1-2-1-1-0" VALUE="242.72" UNIT="V" />
<INSTPARAM CODE="P1-2-2-1-0" VALUE="245.23" UNIT="V" />
<INSTPARAM CODE="P1-2-3-1-0" VALUE="243.08" UNIT="V" />
<INSTPARAM CODE="P2-1-1-1-0" VALUE="0" UNIT="A" />
<INSTPARAM CODE="P2-1-2-1-0" VALUE="0" UNIT="A" />
<INSTPARAM CODE="P2-1-3-1-0" VALUE="0" UNIT="A" />
<INSTPARAM CODE="P8-1-0-0-0" VALUE="RYB" />
<INSTPARAM CODE="P8-2-0-0-0" VALUE="" />
<INSTPARAM CODE="P3-2-1-1-0" VALUE="0" UNIT="k" />
<INSTPARAM CODE="P3-3-1-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P4-1-1-0-0" VALUE="" /><INSTPARAM CODE="P3-2-2-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P3-3-2-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P4-2-1-0-0" VALUE="" /><INSTPARAM CODE="P3-2-3-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P3-3-3-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P4-3-1-0-0" VALUE="" /><INSTPARAM CODE="P3-2-4-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P3-3-4-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P3-4-4-1-0" VALUE="0" UNIT="k" /><INSTPARAM CODE="P4-4-1-0-0" VALUE="" /><INSTPARAM CODE="P9-1-0-0-0" VALUE="50.02" UNIT="Hz" />
</D2>

</UTILITYTYPE>

</CDF>


What I have tried:

From the above code I need to print the all the values like-> P1-2-1-1-0.
result should be such as
P1-2-1-1-0
P1-2-2-1-0
P1-2-3-1-0
P2-1-1-1-0
P2-1-2-1-0
P2-1-3-1-0
P8-1-0-0-0
P8-2-0-0-0
P3-2-1-1-0
P3-3-1-1-0
...
...
...
Posted
Updated 21-Jun-21 1:56am
v2

You could e.g. use Regular Expressions if you just want to get the codes.
var xml = File.ReadAllText("data.xml");
var codes = Regex.Matches(xml, @"P\d-\d-\d-\d-\d");
foreach (var code in codes)
{
    Console.WriteLine(code);
}
   
v3
Comments
Himansh jain 21-Jun-21 3:17am
   
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\himansh.jain\\Downloads\\Xmltesting1.xml");
string pattern = @"P\d-\d-\d-\d-\d";
Regex rgx = new Regex(pattern);
foreach (Match match in rgx.Matches(doc))
Console.WriteLine("'{0}'",match.Value);

i could not make it work , can you please identify the error.
TheRealSteveJudge 21-Jun-21 3:23am
   
You did not follow my suggestion.
Please have a look at the updated solution
Himansh jain 21-Jun-21 3:37am
   
thank you for your solution but still i am not able yo print the result

string path = @"C:\Users\himansh.jain\Downloads\Xmltesting1.xml";
var xml = File.ReadAllText(path);
var codes = Regex.Matches(xml, @"P\d-\d-\d-\d-\d");
Console.WriteLine(codes);
After Running the above code I am getting this...i don't know why i am not able to print the result.
System.Text.RegularExpressions.MatchCollection
C:\Users\himansh.jain\source\repos\readxmls\bin\Debug\netcoreapp3.1\readxmls.exe (process 34808) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
TheRealSteveJudge 21-Jun-21 3:44am
   
That is perfectly normal as Regex.Matches returns a MatchCollection.
You must loop through this MatchCollection.
See updated solution.
Himansh jain 21-Jun-21 3:59am
   
Thank you very much
TheRealSteveJudge 21-Jun-21 4:01am
   
You're welcome! Thank you for accepting the solution.
I'd suggest to use XDocument class[^]:

C#
XDocument xdoc = XDocument.Parse(_xml_text_here_); //or use Load method
List<string> all = xdoc.Descendants("INSTPARAM")
    .Select(x=>x.Attribute("CODE").Value)
    .ToList();

foreach(string s in all)
    Console.WriteLine($"{s}");


For further details, please see: XDocument.Load Method (System.Xml.Linq) | Microsoft Docs[^]
   
v2
Comments
TheRealSteveJudge 21-Jun-21 7:59am
   
This is a more versatile solution! 5*
Maciej Los 21-Jun-21 8:01am
   
Thank you.

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