Reading an XML file
Example on how to read an XML fileTrendIsYourFriend
csharp
2 years ago
4.4 kB
32
No Index
Never
private void TestingMyXMLFunction() { // get nb of stop/target lines defined in the current ATM strategy template int cntAtmBracket = parseAtmXMLfile(); Print( "How many brackets are defined in the ATM template: " + cntAtmBracket.ToString() ); } private string GetATMStrategy() { /// returns a string holding the name of the selected ATM strategy in the chart trader panel /// a null value is returned if no selection has been made #region GETATMSTRATEGY //- string tempATMStrategyName = null; try { AtmStrategy atmStrategy = this.ChartControl.OwnerChart.ChartTrader.AtmStrategy; if (atmStrategy != null) { tempATMStrategyName = atmStrategy.Template + ""; } } catch (Exception ex) { //stuff exception // tempATMStrategyName = null; } return tempATMStrategyName; //- #endregion } private int parseAtmXMLfile() { /// Reads the ATM template XML file and returns the number of targets added by the user /// return -1 if no ATM file specified in chart trader otherwise a valid count #region PARSE_ATM_XML_FILE //- atmTemplateName = GetATMStrategy(); if (atmTemplateName == null) return -1; int bracketKey; double nodeQuantity = 0; double nodeStopLoss = 0; double nodeTarget = 0; string nodeAutoBreakEvenAt = string.Empty; string pathToAtmStratFolder = NinjaTrader.Core.Globals.UserDataDir + "templates\\AtmStrategy\\"; string xmlFileToLoad = pathToAtmStratFolder + atmTemplateName + ".xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load( xmlFileToLoad ); // https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectnodes?redirectedfrom=MSDN&view=net-6.0#System_Xml_XmlNode_SelectNodes_System_String_ // solution found here -> https://stackoverflow.com/questions/12607895/cant-get-xmldocument-selectnodes-to-retrieve-any-of-my-nodes // the XML tag <Brackets>...</Bracket> contains the nodes that define the Stop/Target/StopStrategy var mgr = new XmlNamespaceManager(xmlDoc.NameTable); mgr.AddNamespace("", "http://schemas.microsoft.com/appx/2010/manifest"); XmlNode root = xmlDoc.DocumentElement; XmlNodeList xnList = root.SelectNodes("//*[local-name()='Brackets']/*[local-name()='Bracket']"); // Loop through all Targets and read its content. // Each pair of tag <Bracket>...</Bracket> contains a Target definition (Quantity, Stop, Target, Stop strategy) // start with a fresh new empty list (Quantity, Stop, Target, StopPrice, LimitPrice, OrderStateStop, OrderStateTarget) bracketList.Clear(); bracketKey = 0; orderQty = 0; foreach (XmlNode xn in xnList) { bracketKey++; try { // if node <Quantity> does not exists then return an empty value nodeQuantity = Double.Parse(xn["Quantity"].InnerText); } catch(Exception ex) { nodeQuantity = 0; } try { // if node <StopLoss> does not exists then return an empty value nodeStopLoss = Double.Parse(xn["StopLoss"].InnerText); } catch(Exception ex) { nodeStopLoss = 0; } try { // if node <Target> does not exists then return an empty value nodeTarget = Double.Parse(xn["Target"].InnerText); } catch(Exception ex) { nodeTarget = 0; } bracketList.Add( bracketKey, new CBracket(nodeQuantity, nodeStopLoss, nodeTarget) ); orderQty = orderQty + nodeQuantity; // uncomment the following lines to read the Stop Strategy and Add a new property in the CBracket class definition // try // { // if node <StopStrategy><AutoBreakEvenProfitTrigger> does not exists then return an empty value // nodeAutoBreakEvenAt = xn["StopStrategy"].SelectSingleNode("AutoBreakEvenProfitTrigger").InnerText; // } // catch(Exception ex) { nodeAutoBreakEvenAt = string.Empty; } // string str2Print = String.Format( " Quantity = {0}/ StopLoss = {1}/ Target = {2}/ AutoBreakEvenAt = {3}", nodeQuantity , nodeStopLoss , nodeTarget, nodeAutoBreakEvenAt ); // Print(str2Print); } return xnList.Count; //- #endregion } // parseAtmXMLfile