Hi, in a previous question on here I managed to successfully parse an xml into a logical format using this code:
private static string ParseXMLDS(string xmlDS)
{
try
{
var doc = XDocument.Load(xmlDS);
string Transaction = "";
string Error = "";
StringBuilder Data = new StringBuilder();
var query = from c in doc.Root.Descendants("Data")
select c.Element("FileToCompress");
Data.Append("Decompressed Files:");
foreach (string name in query)
{
Data.AppendLine(name);
}
var query1 = from d in doc.Root.Descendants("Transaction")
select new
{
TransactionID = d.Element("TransactionID").Value,
TransactionDate = d.Element("TransactionDate").Value,
CompressedFileName = d.Element("CompressedFileName").Value,
TransactionSuccessFlag = d.Element("TransactionSuccessFlag").Value,
TransactionErrors = d.Element("TransactionErrors").Value
};
foreach (var details in query1)
{
Transaction = "Transaction Details:" + "\r\n" + Convert.ToString(details) + "\r\n";
}
var query2 = from e in doc.Root.Descendants("Error")
select new
{
TransactionID = e.Element("transactionID").Value,
ErrorMessage = e.Element("ErrorMessage").Value,
InnerException = e.Element("InnerException").Value,
StackTrace = e.Element("StackTrace").Value,
Data = e.Element("Data").Value
};
foreach (var errors in query2)
{
Error = "Error Details:" + "\r\n" + Convert.ToString(errors);
}
Transaction = Transaction.Replace("{", "");
Transaction = Transaction.Replace("}", "");
Transaction = Transaction.Replace(",", "\r\n");
Error = Error.Replace("{", "");
Error = Error.Replace("}", "");
Error = Error.Replace(",", "\r\n");
if (Transaction.Contains("TransactionErrors = No Errors") == true)
{
return Transaction.ToString() + "\r\n" + Data.ToString();
}
else
{
return Error.ToString();
}
}
catch (Exception ex)
{
return ex.Message;
}
}
my problem now arises that whenever I need to read an xml file that contains errors as the layout is slightly different from an XML file with no errors, example of both below:
XML File No Errors:
="1.0"="utf-8"
- <NewDataSet>
- <Error>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<ErrorMessage />
<InnerException />
<StackTrace />
<Data />
</Error>
- <Data>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<TransactionDateTimeStamp>0001-01-01T00:00:00-06:00</TransactionDateTimeStamp>
<FileToCompress>Test.aspx</FileToCompress>
<DirectoryToCompress>C:Inetpub/Test</DirectoryToCompress>
<CompressedFileName>Test.aspx</CompressedFileName>
<Action>Decompress</Action>
</Data>
- <Transaction>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<TransactionDate>2013-10-29T09:14:01.4803735-05:00</TransactionDate>
<CompressedFileName />
<TransactionSuccessFlag>true</TransactionSuccessFlag>
<TransactionErrors>No Errors</TransactionErrors>
</Transaction>
</NewDataSet>
Xml File with Errors:
="1.0"="utf-8"
- <NewDataSet>
- <Error>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<ErrorMessage />
<InnerException />
<StackTrace />
<Data />
</Error>
- <Error>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<ErrorMessage>Object reference not set to an instance of an object.</ErrorMessage>
<StackTrace>at EvalDSConfigsToXMLStreamThreadWithState.ThreadProc() in d:\test\Tools_Apps\XMLReaderWriter.cs:line 268</StackTrace>
<Data>System.Collections.ListDictionaryInternal</Data>
</Error>
- <Data>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<TransactionDateTimeStamp>0001-01-01T00:00:00-06:00</TransactionDateTimeStamp>
<FileToCompress />
<DirectoryToCompress />
<CompressedFileName />
<Action>Error Reading First Directory</Action>
</Data>
- <Transaction>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<TransactionDate>2013-10-29T09:14:01.4803735-05:00</TransactionDate>
<CompressedFileName />
<TransactionSuccessFlag>true</TransactionSuccessFlag>
<TransactionErrors>No Errors</TransactionErrors>
</Transaction>
- <Transaction>
<TransactionID>adf540ec-05b0-49c9-98f5-44eea57c0b27</TransactionID>
<TransactionDate>2013-10-29T09:14:01.4959735-05:00</TransactionDate>
<CompressedFileName />
<TransactionSuccessFlag>false</TransactionSuccessFlag>
<TransactionErrors />
</Transaction>
</NewDataSet>
for this scenario I need to check the <transactionsuccessflag> and if its false output the 2nd Error Node details and the 2nd Transaction Node details. Can anyone please give me some advice?