Parsing an XML File
-
Hi, Can someone help me parse the following XML with DotNet C# 4.0? I'm trying to do it with linq. XML is as follows... 0 "Success" "(Job 1) No Errors detected" 0 "Success" "(Job 2) Scheduling Complete. All available orders were planned." "" "A24" "" "" "" "" "XXX" "Run-0001" 1 4 0 0.00000 0.00000 "" "" "" "" "" 0.00 0.00 "XXX" "00:00:00" "00:00:00" "00:00:00" "00:00:00" 0 0 0 0 0 "Mon" "19:00:00" "Mon" "19:00:00" 0 0 0.000 0.0000 2 "" "A24" "A24" 61.000 102 102 "PDA3" "761" "YYY" "111" "222" "0.00" "0.00" "XXX" "Run-0001" 2 2 2 0.00000 0.00000 "" "" "" "" <
-
Hi, Can someone help me parse the following XML with DotNet C# 4.0? I'm trying to do it with linq. XML is as follows... 0 "Success" "(Job 1) No Errors detected" 0 "Success" "(Job 2) Scheduling Complete. All available orders were planned." "" "A24" "" "" "" "" "XXX" "Run-0001" 1 4 0 0.00000 0.00000 "" "" "" "" "" 0.00 0.00 "XXX" "00:00:00" "00:00:00" "00:00:00" "00:00:00" 0 0 0 0 0 "Mon" "19:00:00" "Mon" "19:00:00" 0 0 0.000 0.0000 2 "" "A24" "A24" 61.000 102 102 "PDA3" "761" "YYY" "111" "222" "0.00" "0.00" "XXX" "Run-0001" 2 2 2 0.00000 0.00000 "" "" "" "" <
You're calling
.Elements("row")
on theXDocument
instance. The document only contains a single element, which is the root element of the XML document. You'll need to use theDescendants
method to find the rows. You'll also need to restrict the scope to the relevantUSEROUT
node, since therow
elements under the first node don't match your expected layout.List jobList = XDocument.Parse(responseString)
.Descendants("USEROUT")
.Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
.Descendants("row")
.Select(item => new Callout { ... })
.ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You're calling
.Elements("row")
on theXDocument
instance. The document only contains a single element, which is the root element of the XML document. You'll need to use theDescendants
method to find the rows. You'll also need to restrict the scope to the relevantUSEROUT
node, since therow
elements under the first node don't match your expected layout.List jobList = XDocument.Parse(responseString)
.Descendants("USEROUT")
.Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
.Descendants("row")
.Select(item => new Callout { ... })
.ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard, Firstly, thanks for taking the time to respond. You answer was exactly what I needed - many thanks for this. One quick question... Is it possible to add some sort of where clause into it so that only those rows with a TASK_TYPE = "2" are returned? Sorry, I'm new to parsing XML... :)
-
Richard, Firstly, thanks for taking the time to respond. You answer was exactly what I needed - many thanks for this. One quick question... Is it possible to add some sort of where clause into it so that only those rows with a TASK_TYPE = "2" are returned? Sorry, I'm new to parsing XML... :)
Something like this should do the trick:
List<Callout> jobList = XDocument.Parse(responseString)
.Descendants("USEROUT")
.Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
.Descendants("row")
.Where(item => string.Equals((string)item.Attribute("TASK_TYPE"), "2", StringComparison.Ordinal))
.Select(item => new Callout { ... })
.ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Something like this should do the trick:
List<Callout> jobList = XDocument.Parse(responseString)
.Descendants("USEROUT")
.Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
.Descendants("row")
.Where(item => string.Equals((string)item.Attribute("TASK_TYPE"), "2", StringComparison.Ordinal))
.Select(item => new Callout { ... })
.ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer