Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have this XML File which is generated to capture the logging information from a decompression action:

XML
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Error>
<transactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</transactionID>
<ErrorMessage />
<InnerException />
<StackTrace />
<Data />
</Error>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress />
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>Read First Directory</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>CoverageUpdate.aspx</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>CustomError.aspx</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>ExpandedCoverage.aspx</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>Global.asax</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>ListControlDispItems.xml</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>PremiumPolicyCoverageCodes.xml</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Data>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDateTimeStamp>2013-10-11T12:45:51.9099748+01:00</TransactionDateTimeStamp>
<FileToCompress>AICS_Client\wait.html</FileToCompress>
<DirectoryToCompress>C:\inetpub\CTSNGCFI</DirectoryToCompress>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<Action>De-Compress</Action>
</Data>
<Transaction>
<TransactionID>44c6e39a-9b94-4e2d-8419-597677a71dcf</TransactionID>
<TransactionDate>2013-10-11T13:45:51.9219748+01:00</TransactionDate>
<CompressedFileName>C:\inetpub\CTSNGCFI\CTSNGCFI.gz</CompressedFileName>
<TransactionSuccessFlag>true</TransactionSuccessFlag>
<TransactionErrors>No Errors</TransactionErrors>
</Transaction>
</NewDataSet>

From the above XML I'd like to extract the following information:

Transaction Date: 2013-10-11T13:37:47.3189748+01:00

Compressed File Name: C:\inetpub\AllstateCTSNGCFI\AllstateCTSNGCFI.gz

Transaction Success: true

Transaction Errors: No Errors



Decompressed Files(<filetocompress>):

CoverageUpdate.aspx

CustomError.aspx

ExpandedCoverage.aspx

Global.asax

bin\Allstate.CTS.SCDBPC.ProductCatalog.SCs.dll

etc



An

And if there are any errors, output the contents from the Error node.


Any help or pointers to tutorials that will help me would be greatly appreciated.
Posted
Comments
pmcm 16-Oct-13 7:00am    
I have been trying to do this method:
private bool ParseXMLDS(string xmlDS)
{
XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.LoadXml(xmlDS);
xmlDoc.LoadXml(xmlDS.Substring(xmlDS.IndexOf(Environment.NewLine)));

string xpath = "NewDataSet/Data ";
var nodes = xmlDoc.SelectNodes(xpath);
StringBuilder compressedFile = new StringBuilder();

foreach (XmlNode childrenNode in nodes)
{
compressedFile.AppendLine(childrenNode.SelectSingleNode("//FileToCompress").Value);
}

if (m_Logger != null)
m_Logger(string.Format("File Decompression Successfully Completed." + '\n' + "{0}", compressedFile));


return true;
}

but I'm getting this error: {"StartIndex cannot be less than zero.\r\nParameter name: startIndex"}.
I'm actually converting a dataset into XML to do this parsing - is this the right thing to do?

You can use XML Serialization and Deserialization.

See below link to see how to use XML Serialization and Deserialization.

XML Serialization and Deserialization: Part-1[^]
 
Share this answer
 
The error in your comment-added code indicates that the problem is with xmlIDS.Substring()
The xmlIDS.IndexOf(Environment.NewLine) failed, so it returned -1 which is an invalid startIndex parameter value for Substring.
Are you sure that the representation of the newline in the xmlIDS string is the same as Environment.NewLine?
And, are you sure that there actually IS a newline in xmlIDS?
 
Share this answer
 
Comments
pmcm 22-Oct-13 6:35am    
Hi I got round the above error by doing this

private static string ParseXMLDS(string xmlDS)
{
try
{
var doc = XDocument.Load(xmlDS);
StringBuilder Transaction = new StringBuilder();
StringBuilder Data = new StringBuilder();
StringBuilder Errors = new StringBuilder();

// Query the Tansaction tags data and write out a subset of info
var Transquery = from b in doc.Root.Descendants("Transaction")
select b.Element("TransactionDate").Value + "/r/n" +
b.Element("CompressedFileName") + "/r/n" +
b.Element("TransactionSuccessFlag").Value + "/r/n" +
b.Element("TransactionErrors").Value;

foreach (string info in Transquery) Transaction.AppendLine(info);


//Query the Data tags and write out a list of decompressed files
var Dataquery = from c in doc.Root.Descendants("Data")
select c.Element("FileToCompress").Value +
c.Element("CompressedFileName").Value;

foreach (string name in Dataquery) Data.AppendLine(name);

// Query the Data tags and write out a list of decompressed files
var Errorsquery = from c in doc.Root.Descendants("Error")
select c.Element("transactionID").Value +
c.Element("ErrorMessage").Value +
c.Element("InnerException").Value +
c.Element("StackTrace").Value +
c.Element("Data").Value;

foreach (string error in Errorsquery) Errors.AppendLine(error);



return Data.ToString();
}
instead having 3 seperate Linq queries can I join the queries and return the seletected info?
Matt T Heffron 22-Oct-13 13:10pm    
Since it appears that you never actually use the Transaction or Errors StringBuilder objects you construct, you can ignore the first and last query entirely!
...OK, I guess that you've edited the code here just to show the queries, I'll assume that you DO actually need those results.
I don't see any way to combine the queries, as each is traversing different elements of the XML document.
(BTW. you have the end-of-line strings mis-defined: they should be BACKslashes: "\r\n"; and in the 2nd and 3rd queries you append the values together without any delimiters.)

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