public OnixLegacyParser(string LegacyOnixContent, bool ExecuteValidation, bool PreprocessOnixFile = true, bool LoadEntireFileIntoMemory = false) { if (String.IsNullOrEmpty(LegacyOnixContent)) { throw new Exception("ERROR! ONIX content provided is empty."); } AlwaysReturnInputXml = false; this.ParserFileContent = new StringBuilder(LegacyOnixContent); this.ParserRVWFlag = true; this.PerformValidFlag = ExecuteValidation; if (PreprocessOnixFile) { ParserFileContent.ReplaceIsoLatinEncodings(); ParserFileContent.FilterIncompleteEncodings(); } // this.LegacyOnixReader = CreateXmlReader(this.ParserFileContent, this.ParserRVWFlag, this.PerformValidFlag); this.LegacyOnixReader = CreateXmlTextReader(this.ParserFileContent); bool ReferenceVersion = DetectVersionReference(ParserFileContent); string sOnixMsgTag = ReferenceVersion ? CONST_ONIX_MESSAGE_REFERENCE_TAG : CONST_ONIX_MESSAGE_SHORT_TAG; this.ParserRefVerFlag = ReferenceVersion; if (LoadEntireFileIntoMemory) { this.LegacyOnixMessage = CreateOnixXmlMsgSerializer(sOnixMsgTag).Deserialize(this.LegacyOnixReader) as OnixLegacyMessage; } else { this.LegacyOnixMessage = null; } }
public OnixLegacyParser(FileInfo LegacyOnixFilepath, bool ExecuteValidation, bool PreprocessOnixFile = true, bool LoadEntireFileIntoMemory = false) { if (!File.Exists(LegacyOnixFilepath.FullName)) { throw new Exception("ERROR! File(" + LegacyOnixFilepath + ") does not exist."); } AlwaysReturnInputXml = false; this.ParserFileInfo = LegacyOnixFilepath; this.ParserRVWFlag = true; this.PerformValidFlag = ExecuteValidation; if (PreprocessOnixFile) { LegacyOnixFilepath.ReplaceIsoLatinEncodings(true); } // this.LegacyOnixReader = CreateXmlReader(this.ParserFileInfo, this.ParserRVWFlag, this.PerformValidFlag); this.LegacyOnixReader = CreateXmlTextReader(this.ParserFileInfo); bool ReferenceVersion = DetectVersionReference(LegacyOnixFilepath); string sOnixMsgTag = ReferenceVersion ? CONST_ONIX_MESSAGE_REFERENCE_TAG : CONST_ONIX_MESSAGE_SHORT_TAG; this.ParserRefVerFlag = ReferenceVersion; if (LoadEntireFileIntoMemory) { this.LegacyOnixMessage = CreateOnixXmlMsgSerializer(sOnixMsgTag).Deserialize(this.LegacyOnixReader) as OnixLegacyMessage; } else { this.LegacyOnixMessage = null; } }
static void Main(string[] args) { try { int nLegacyShortIdx = 0; int nLegacyPrdIdx = 0; int nOnixPrdIdx = 0; string sLegShortFile = @"C:\tmp\tmp2\onix.legacy.short.xml"; string sLegacyXml = File.ReadAllText(@"C:\tmp\tmp2\onix.legacy.xml"); string sOnixXml = File.ReadAllText(@"C:\tmp\tmp2\onix.sample.v3.xml"); string sLegacyShortXml = File.ReadAllText(@"C:\tmp\tmp2\onix.legacy.short.xml"); string sEan = "117"; bool bIsValid = sEan.IsValidEAN(); if (true) { // using (OnixLegacyParser onixLegacyShortParser = new OnixLegacyParser(new FileInfo(sLegShortFile), true)) // using (OnixLegacyParser onixLegacyShortParser = new OnixLegacyParser(new FileInfo(sLegShortFile), true, false, false)) using (OnixLegacyParser onixLegacyShortParser = new OnixLegacyParser(sLegacyShortXml, true, false, false)) { bool ValidFile = onixLegacyShortParser.ValidateFile(); OnixLegacyHeader Header = onixLegacyShortParser.MessageHeader; foreach (OnixLegacyProduct TmpProduct in onixLegacyShortParser) { int x = 0; // string[] TypeCodes = TmpProduct.OnixEditionTypeCodeList; if ((TmpProduct.OnixContributorList != null) && (TmpProduct.OnixContributorList.Length > 0)) { foreach (OnixLegacyContributor TempContributor in TmpProduct.OnixContributorList) { System.Console.WriteLine("Key Names (" + TempContributor.OnixKeyNames + "), Names before Key (" + TempContributor.OnixNamesBeforeKey + ")"); } } if ((TmpProduct.OnixAudRangeList != null) && (TmpProduct.OnixAudRangeList.Length > 0)) { OnixLegacyAudRange AudRange = TmpProduct.OnixAudRangeList[0]; string AgeFrom = AudRange.USAgeFrom; string AgeTo = AudRange.USAgeTo; System.Console.WriteLine("AgeFrom(" + AgeFrom + "), AgeTo(" + AgeTo + ")"); } if (TmpProduct.IsValid()) { string sEANStatus = TmpProduct.HasValidEAN() ? "valid" : "invalid"; string sISBNStatus = TmpProduct.HasValidISBN() ? "valid" : "invalid"; if (TmpProduct.HasValidEAN()) { string sTestISBN = TmpProduct.EAN.ConvertEANToISBN(); } if (TmpProduct.HasValidISBN()) { string sTestEAN = TmpProduct.ISBN.ConvertISBNToEAN(); } System.Console.WriteLine("Product [" + (nLegacyShortIdx++) + "] has " + sEANStatus + " EAN(" + TmpProduct.EAN + "), " + sISBNStatus + " ISBN(" + TmpProduct.ISBN + "), and USD Retail Price(" + TmpProduct.USDRetailPrice.PriceAmount + ") - HasUSRights(" + TmpProduct.HasUSRights() + ")."); } else { System.Console.WriteLine(TmpProduct.GetParsingError()); } } } } if (true) { FileInfo LegacyFileInfo = new FileInfo(@"C:\tmp\tmp2\onix.legacy.xml"); using (OnixLegacyParser LegacyParser = new OnixLegacyParser(LegacyFileInfo, true, false)) { OnixLegacyHeader Header = LegacyParser.MessageHeader; foreach (OnixLegacyProduct TmpProduct in LegacyParser) { // string[] TypeCodes = TmpProduct.OnixEditionTypeCodeList; if ((TmpProduct.OnixAudRangeList != null) && (TmpProduct.OnixAudRangeList.Length > 0)) { OnixLegacyAudRange AudRange = TmpProduct.OnixAudRangeList[0]; string AgeFrom = AudRange.USAgeFrom; string AgeTo = AudRange.USAgeTo; System.Console.WriteLine("AgeFrom(" + AgeFrom + "), AgeTo(" + AgeTo + ")"); } if (TmpProduct.IsValid()) { System.Console.WriteLine("Product [" + (nLegacyPrdIdx++) + "] has EAN(" + TmpProduct.EAN + ") and USD Retail Price(" + TmpProduct.USDRetailPrice.PriceAmount + ") - HasUSRights(" + TmpProduct.HasUSRights() + ")."); } else { System.Console.WriteLine(TmpProduct.GetParsingError()); } } } } if (true) { nOnixPrdIdx = 0; FileInfo CurrentFileInfo = new FileInfo(@"C:\tmp\tmp2\onix.sample.v3.xml"); using (OnixParser V3Parser = new OnixParser(CurrentFileInfo, true)) { OnixHeader Header = V3Parser.MessageHeader; foreach (OnixProduct TmpProduct in V3Parser) { long lTempEAN = TmpProduct.EAN; string sTempISBN = TmpProduct.ISBN; OnixData.Version3.Price.OnixPrice USDPrice = TmpProduct.USDRetailPrice; // string[] TypeCodes = TmpProduct.OnixEditionTypeCodeList; if (TmpProduct.IsValid()) { System.Console.WriteLine("Product [" + (nLegacyPrdIdx++) + "] has EAN(" + TmpProduct.EAN + ") and USD Retail Price(" + TmpProduct.USDRetailPrice.PriceAmount + ") - HasUSRights(" + TmpProduct.HasUSRights() + ")."); } else { System.Console.WriteLine(TmpProduct.GetParsingError()); } } } } if (true) { nOnixPrdIdx = 0; FileInfo CurrentFileInfo = new FileInfo(@"C:\tmp\tmp2\onix.sample.v3.short.xml"); using (OnixParser V3Parser = new OnixParser(true, CurrentFileInfo, false, false)) { OnixHeader Header = V3Parser.MessageHeader; foreach (OnixProduct TmpProduct in V3Parser) { long lTempEAN = TmpProduct.EAN; string sTempISBN = TmpProduct.ISBN; OnixContributor MainAuthor = TmpProduct.PrimaryAuthor; if (TmpProduct.HasUSDRetailPrice()) { OnixData.Version3.Price.OnixPrice USDPrice = TmpProduct.USDRetailPrice; } // string[] TypeCodes = TmpProduct.OnixEditionTypeCodeList; if (TmpProduct.IsValid()) { System.Console.WriteLine("Product [" + (nLegacyPrdIdx++) + "] has EAN(" + TmpProduct.EAN + ") and USD Retail Price(" + TmpProduct.USDRetailPrice.PriceAmount + ") - HasUSRights(" + TmpProduct.HasUSRights() + ")."); } else { System.Console.WriteLine(TmpProduct.GetParsingError()); } } } } if (false) { OnixLegacyMessage onixLegacyMessage = sLegacyXml.ParseXML <OnixLegacyMessage>(); foreach (OnixLegacyProduct TmpProduct in onixLegacyMessage.Product) { System.Console.WriteLine("Product [" + (nLegacyPrdIdx++) + "] has EAN(" + TmpProduct.EAN + ") and USD Retail Price(" + TmpProduct.USDRetailPrice.PriceAmount + ") - HasUSRights(" + TmpProduct.HasUSRights() + ")."); } OnixMessage onixMessage = sOnixXml.ParseXML <OnixMessage>(); foreach (OnixProduct TmpProduct in onixMessage.Product) { System.Console.WriteLine("Product [" + (nOnixPrdIdx++) + "] has EAN(" + TmpProduct.EAN + ") and USD Retail Price(" + TmpProduct.USDRetailPrice.PriceAmount + ") - HasUSRights(" + TmpProduct.HasUSRights() + ")."); } } } catch (Exception ex) { System.Console.WriteLine(ex); } }