Click here to Skip to main content
14,972,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, in a previous question on here I managed to successfully parse an xml into a logical format using this code:
C#
private static string ParseXMLDS(string xmlDS)
{
    try
    {
        var doc = XDocument.Load(xmlDS);
        string Transaction = "";
        string Error = "";
        StringBuilder Data = new StringBuilder();

        // Query the Data section and write out FileToCompress value
        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);
        }


        // Query the Transaction section and write out a subset of Transaction
        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";
        }

        // Query the Error section and write out a subset of Error
        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);
        }

        //Format the Strings so output is not all on one line
        Transaction = Transaction.Replace("{", "");
        Transaction = Transaction.Replace("}", "");
        Transaction = Transaction.Replace(",", "\r\n");
        Error = Error.Replace("{", "");
        Error = Error.Replace("}", "");
        Error = Error.Replace(",", "\r\n");

        //Check Transaction details for Errors
        if (Transaction.Contains("TransactionErrors = No Errors") == true)
        {
            //No Errors return Transaction & Data info 
            return Transaction.ToString() + "\r\n" + Data.ToString();
        }
        else
        {
            //Errors return Error info
            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:
XML
  <?xml version="1.0" encoding="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:
XML
  <?xml version="1.0" encoding="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?
Posted
Comments
pmcm 7-Nov-13 11:15am
   
Ok I'm another step closer I think to resolving this, I've added a counter to the number of Transaction nodes returned and from here I would like to select the 2nd Error node but this updated linq query is not quite right:
<pre lang="c#">
int counter = 0;

// Query the Transaction element and write out a subset of Transaction
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";
counter++;
}

if (counter > 1)
{
// Query the data and write out a subset of Error
var query2 = from e in doc.Root.Descendants("Error")
where e.Element("ErrorMessage") != null
select new
{
TransactionID = (string)e.Element("TransactionID").Value,
ErrorMessage = (string)e.Element("ErrorMessage") ?? String.Empty,
InnerException = (string)e.Element("InnerException") ?? String.Empty,
StackTrace = (string)e.Element("StackTrace") ?? String.Empty,
Data = (string)e.Element("Data") ?? String.Empty,
};

foreach (var errors in query2)
{
Error = "Error Details:" + "\r\n" + Convert.ToString(errors);
}
}
</pre>
why am I not getting the 2nd Error node details whenever I'm doing my select on where e.Element("ErrorMessage") != null

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