I'm downloading some CSV data as part of an API response and the last column of the CSV data contains an XML snippet as below:
<tns:Event xmlns:tns="http://someurl">
<tns:MaximumSeverity.Code>Error</tns:MaximumSeverity.Code>
<tns:EventItems>
<tns:EventItem>
<tns:Error.Code>ERRORCODE</tns:Error.Code>
<tns:Severity.Code>Error</tns:Severity.Code>
<tns:Short.Description>Short error</tns:Short.Description>
<tns:Detailed.Description>Longer error</tns:Detailed.Description>
<tns:Parameters></tns:Parameters>
</tns:EventItem>
</tns:EventItems>
</tns:Event>
I'd like to extract the text from either the short or detailed description so I'm wondering what the most efficient way is of doing this?
There could be multiple EventItem elements within the XML.
I tried the code below but got an XPathException:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XmlNodeList nodeList = doc.SelectNodes("/tns:Event");
foreach (XmlNode node in nodeList)
{
string errorTxt = node.SelectSingleNode("Short.Description").InnerText;
Console.WriteLine(errorTxt);
}
Console.ReadKey();