0

Im just new to c# xml so I need to ask some help to parse a certain xml telegram. Basically what I need is to loop in each nodes to get the UID, ID and value is there a way to do it using LINQ or any other approach?

<pre> PROCESSDATA,ALSS_L01S01,10,20140721005835,<ProcessData tokens="14" cid="1234" uid_1="1234" result_1="P" uid_2="5655" result_2="P" uid_3="8487" result_3="P" uid_4="73695" result_4="P" uid_5="2365" result_5="P" uid_6="12584" result_6="P"> <TestPlans> <TestPlan Name="FOO" Group="FOOFRGP"> <TestRun Uid="1234" UidType="SN"> <Test Id="INS" Value="270.50"/> </TestRun> <TestRun Uid="5655" UidType="SN"> <Test Id="INS" Value="260.50"/> </TestRun> <TestRun Uid="8487" UidType="SN"> <Test Id="INS" Value="260.50"/> </TestRun> <TestRun Uid="73695" UidType="SN"> <Test Id="INS" Value="260.50"/> </TestRun> <TestRun Uid="2365" UidType="SN"> <Test Id="INS" Value="260.50"/> </TestRun> <TestRun Uid="12584" UidType="PCB_SN"> <Test Id="INS" Value="260.50"/> </TestRun> </TestPlan> </TestPlans> </ProcessData> </pre> 
3
  • Your XML is invalid. What is PROCESSDATA,ALSS_L01S01,10,20140721005835,? Probably, it simply does not belong to here. Commented Nov 19, 2015 at 9:07
  • @YeldarKurmangaliyev the XML is well-formed, but I do agree that the content looks odd. Commented Nov 19, 2015 at 9:11
  • Oh, yes, I am wrong. However, it really looks strange. Make sure that it is exactly what you have. Commented Nov 19, 2015 at 9:14

2 Answers 2

1

A rather simplistic approach could be done like this, assuming that the xmlString variable holds the XML you posted in your question:

 var xDocument = XDocument.Parse(xmlString); var testRunElements = xDocument.Descendants("TestRun"); foreach (var testRunElement in testRunElements) { var uid = testRunElement.Attribute("Uid").Value; var testElement = testRunElement.Element("Test"); var id = testElement.Attribute("Id").Value; var value = testElement.Attribute("Value").Value; } 

The xDocument.Descendants selects the elements named TestRun. Then we use the foreach to iterate through each element to extract the values from the named attributes.

For safety you'd probably want to check that elements and attributes are not null before attempting to access any properties, in case your actual XML isn't guaranteed to have this schema for each TestRun element.

Sign up to request clarification or add additional context in comments.

Comments

0

You can make class from that i usually use http://xmltocsharp.azurewebsites.net/ (or use Visual Studio Function XML to CS (https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx)) then you can deserialize that XML to an object

public static T Deserialize<T>(string path) { XmlSerializer ser = new XmlSerializer(typeof(T)); object result; using (FileStream reader = File.Open(path, FileMode.Open)) { result = ser.Deserialize(reader); } return (T)result; } 

Link to serialization: https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx


string path = path to file you want to load. T is output Object

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.