/// <summary> /// Sets fields for object from Xml file. /// </summary> /// <param name="reader">Reader module of file with data.</param> public void Read(XmlTextReader reader) { while (reader.Read()) { if (reader.Name == "id") { this.id = reader.ReadElementContentAsInt(); } if (reader.Name == "name") { this.name = reader.ReadElementContentAsString(); } if (reader.Name == "price") { this.price = reader.ReadElementContentAsDouble(); break; } } }
/// <summary> /// Gets price by id /// </summary> /// <param name="identifier"> id of searching product</param> /// <param name="reader">fie to read</param> /// <returns>price of product with searching id</returns> public double PriceById(int identifier) { XmlTextReader reader = new XmlTextReader("XMLFile1.xml"); int tempId = 0; while (reader.Read()) { if (reader.Name == "id") { tempId = reader.ReadElementContentAsInt(); } if (reader.Name == "price") { this.price = reader.ReadElementContentAsDouble(); if (identifier == tempId) { return this.price; } } } return 0.0; }
void LoadConfigFile() { try { XmlTextReader doc = new XmlTextReader("reddit_api.xml"); while (doc.Read()) { if (doc.NodeType == XmlNodeType.Element) { switch (doc.Name) { case "seconds_between_api_calls": m_seconds_between_calls = doc.ReadElementContentAsDouble(); break; case "seconds_before_cache_invalid": m_seconds_before_cache_invalid = doc.ReadElementContentAsDouble(); break; case "default_content_limit": m_content_limit = doc.ReadElementContentAsInt(); break; case "object_mappings": string kind = "", objectname = ""; while (doc.MoveToNextAttribute()) { if (doc.Name == "kind") { kind = doc.Value; } else if (doc.Name == "object") { objectname = doc.Value; } } if (m_object_mapping.ContainsKey(kind)) m_object_mapping["kind"] = objectname; else m_object_mapping.Add(kind, objectname); break; case "domain": m_domain = doc.ReadElementContentAsString(); break; } } } doc.Close(); } catch (FileNotFoundException) { StreamWriter s = new StreamWriter("reddit_api.xml"); s.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); s.WriteLine("<reddit_api_config>"); s.WriteLine("<seconds_between_api_calls>2</seconds_between_api_calls>" ); s.WriteLine("<seconds_before_cache_invalid>30</seconds_before_cache_invalid>"); s.WriteLine("<default_content_limit>25</default_content_limit>"); s.WriteLine("<object_mappings kind=\"comment_kind\" object=\"t1\"/>"); s.WriteLine("<object_mappings kind=\"message_kind\" object=\"t4\"/>"); s.WriteLine("<object_mappings kind=\"more_kind\" object =\"more\"/>"); s.WriteLine("<object_mappings kind=\"redditor_kind\" object=\"t2\"/>"); s.WriteLine("<object_mappings kind=\"submission_kind\" object=\"t3\"/>"); s.WriteLine("<object_mappings kind=\"subreddit_kind\" object=\"t5\"/>"); s.WriteLine("<object_mappings kind=\"userlist_kind\" object=\"UserList\"/>"); s.WriteLine("<domain>http://www.reddit.com/</domain>"); s.WriteLine("<!--"); s.WriteLine("message_kind: t7"); s.WriteLine("submission_kind: t6"); s.WriteLine("subreddit_kind: t5"); s.WriteLine(" -->"); s.WriteLine("</reddit_api_config>"); s.Close(); LoadConfigFile(); } }
private static void ParseXmlSnip(string xmlSnipet) { #region XML snipet Example /* <?xml version="1.0" encoding="utf-16"?> <extendedInfo> <property> <propertyName>Feature ID 2</propertyName> <propertyValue>1</propertyValue> </property> <property> <propertyName>Distance</propertyName> <propertyValue>8.27 metres</propertyValue> </property> <property> <propertyName>Feature class 2</propertyName> <propertyValue>TA_INTEREST_PARCEL_SHAPES</propertyValue> </property> <property> <propertyName>To point y</propertyName> <propertyValue>56.6507597761194</propertyValue> </property> <property> <propertyName>From point x</propertyName> <propertyValue>-128.190507865672</propertyValue> </property> <property> <propertyName>Feature ID 1</propertyName> <propertyValue>10</propertyValue> </property> <property> <propertyName>To point x</propertyName> <propertyValue>-128.190565179104</propertyValue> </property> <property> <propertyName>From point y</propertyName> <propertyValue>56.650826880597</propertyValue> </property> <property> <propertyName>Feature class 1</propertyName> <propertyValue>TA_ADMIN_AREA_SHAPES</propertyValue> </property> </extendedInfo> */ #endregion XmlTextReader reader = new XmlTextReader(new StringReader(xmlSnipet)); reader.WhitespaceHandling = WhitespaceHandling.None; while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Text: switch (reader.Value) { case "Feature ID 2": reader.Read(); reader.ReadEndElement(); _f2_id = reader.ReadElementContentAsInt(); break; case "Feature ID 1": reader.Read(); reader.ReadEndElement(); _f1_id = reader.ReadElementContentAsInt(); break; case "Distance": break; case "To point y": reader.Read(); reader.ReadEndElement(); _f1_y = reader.ReadElementContentAsDouble(); break; case "To point x": reader.Read(); reader.ReadEndElement(); _f1_x = reader.ReadElementContentAsDouble(); break; case "From point y": reader.Read(); reader.ReadEndElement(); _f2_y = reader.ReadElementContentAsDouble(); break; case "From point x": reader.Read(); reader.ReadEndElement(); _f2_x = reader.ReadElementContentAsDouble(); break; case "Feature class 2": reader.Read(); reader.ReadEndElement(); _f2_class = reader.ReadElementContentAsString(); break; case "Feature class 1": reader.Read(); reader.ReadEndElement(); _f1_class = reader.ReadElementContentAsString(); break; } break; } } }
private void ReadColumn(XmlTextReader reader, int depth) { string modify_default = null; string title = null; string long_title = null; string sort_key = null; double width = -1; int max_width = -1; int min_width = -1; bool visible = true; string renderer_type = null; string renderer_property = null; bool renderer_expand = true; while (reader.MoveToNextAttribute ()) { if (reader.Name == "modify-default") { modify_default = reader.Value; break; } } while (reader.Read ()) { if (reader.NodeType == XmlNodeType.EndElement && reader.Depth == depth) { break; } else if (reader.NodeType != XmlNodeType.Element) { continue; } switch (reader.Name) { case "title": title = reader.ReadString (); break; case "long-title": long_title = reader.ReadString (); break; case "sort-key": sort_key = reader.ReadString (); break; case "width": width = reader.ReadElementContentAsDouble (); break; case "max-width": max_width = reader.ReadElementContentAsInt (); break; case "min-width": min_width = reader.ReadElementContentAsInt (); break; case "visible": visible = ParseBoolean (reader.ReadString ()); break; case "renderer": while (reader.MoveToNextAttribute ()) { switch (reader.Name) { case "type": renderer_type = reader.Value; break; case "property": renderer_property = reader.Value; break; case "expand": renderer_expand = ParseBoolean (reader.Value); break; } } break; } } if (modify_default != null) { Column column = GetDefaultColumn (modify_default); if (title != null) { column.Title = title; } if (long_title != null) { column.LongTitle = long_title; } if (renderer_type != null) { ColumnCell renderer = GetCellRenderer (renderer_type, renderer_property, renderer_expand); column.RemoveCell (0); column.PackStart (renderer); } if (renderer_property != null) { column.GetCell (0).Property = renderer_property; } if (column.Visible != visible) { column.Visible = visible; } if (column is SortableColumn && sort_key != null) { ((SortableColumn)column).SortKey = sort_key; } } else { ColumnCell renderer = GetCellRenderer (renderer_type, renderer_property, renderer_expand); Column column = sort_key == null ? new Column (title, renderer, width, visible) : new SortableColumn (title, renderer, width, sort_key, visible); if (max_width != -1) { column.MaxWidth = max_width; } if (min_width != -1) { column.MinWidth = min_width; } Add (column); } }
/// <summary> /// Loads in the joint definitions from an xml based file (Returns 0=Success / -1= Failed to find file) /// </summary> /// <param name="file">The file containing the joint definitions</param> /// <returns></returns> public static int LoadJointDefinition(string file) { Joint newJoint = null; int result = 0; jointList = null; jointList = new List<Joint>(); // START Added for MEng Project jointCoRList = new List<CoRLocator>(); try { //Create the xml reader XmlTextReader xmlJoints = new XmlTextReader(file); //While reading while (xmlJoints.Read()) { //Get the node type XmlNodeType nodeType = xmlJoints.NodeType; if (nodeType == XmlNodeType.Element) // If its an element { switch (xmlJoints.Name) { case "joint": // New joint newJoint = new Joint(); break; case "id": newJoint.ID = xmlJoints.ReadElementContentAsInt(); break; case "name": newJoint.Name = xmlJoints.ReadElementContentAsString(); break; case "trackable1": newJoint.Trackable1 = xmlJoints.ReadElementContentAsInt(); break; case "trackable2": newJoint.Trackable2 = xmlJoints.ReadElementContentAsInt(); break; case "yawOffset": newJoint.YawOffset = xmlJoints.ReadElementContentAsDouble(); break; case "pitchOffset": newJoint.PitchOffset = xmlJoints.ReadElementContentAsDouble(); break; case "rollOffset": newJoint.RollOffset = xmlJoints.ReadElementContentAsDouble(); break; } } // If its the joint end element add the joint to the joint list if (nodeType == XmlNodeType.EndElement && xmlJoints.Name == "joint") { jointList.Add(newJoint); // START Added for MEng Project // Add a corrisponding CoR Locator to the list jointCoRList.Add(new CoRLocator()); // END Added for MEng Project } } } catch { result = -1; // File not found; } return result; }
public void LoadProducts() { XmlTextReader reader = new XmlTextReader(this.ProductsPath); Product p = new Product(); int id = -1; string name = "no_name"; double price = -1.0; while (reader.Read()) { if (reader.Name == "ProductId" && reader.HasAttributes) { id = Convert.ToInt32(reader.GetAttribute("product_id")); } if (reader.Name == "ProductName") { name = reader.ReadElementContentAsString(); } if (reader.Name == "ProductPrice") { price = reader.ReadElementContentAsDouble(); this.products.Add(new Product(id, name, price)); Console.WriteLine(1); } } reader.Close(); }
private static List<double> GetParameterFSR(string configFile, string parName) { FileStream configParPtr = new FileStream(configFile, FileMode.Open, FileAccess.Read); using (XmlTextReader reader = new XmlTextReader(configParPtr)) { reader.ReadToFollowing("ParameterSet"); while (reader.ReadToFollowing("Parameter")) { reader.MoveToFirstAttribute(); if (reader.Value == parName) { List<double> FSR = new List<double>(); reader.ReadToFollowing("BaseUnit"); string parameterUnit = reader.ReadElementContentAsString(); reader.ReadToFollowing("DataFormat"); if (parameterUnit == "BitVector")//Status registers show in binary form { FSR.Add(2); FSR.Add(65535); FSR.Add(0); } else if (parameterUnit == "Count") //to handle counters { FSR.Add(1); FSR.Add(65535); FSR.Add(0); } else { string parameterFormat = reader.ReadElementContentAsString(); if (parameterFormat == "OffsetBinary") //check if its data parameter or status register { FSR.Add(0); reader.ReadToFollowing("RangeMaximum"); FSR.Add(reader.ReadElementContentAsDouble()); //List<double> FSR = new List<double> { reader.ReadElementContentAsDouble() }; reader.ReadToFollowing("RangeMinimum"); FSR.Add(reader.ReadElementContentAsDouble()); //Console.WriteLine("Parameter {0} Max: {1}, Min: {2} ", parName,FSR[0], FSR[1]); } else if (parameterFormat == "BinaryCodedDecimal") { if (parameterUnit == "Celsius") { FSR.Add(3); reader.ReadToFollowing("RangeMaximum"); FSR.Add(reader.ReadElementContentAsDouble()); //List<double> FSR = new List<double> { reader.ReadElementContentAsDouble() }; reader.ReadToFollowing("RangeMinimum"); FSR.Add(reader.ReadElementContentAsDouble()); } else { FSR.Add(4); FSR.Add(65535); FSR.Add(0); } } else { FSR.Add(4); FSR.Add(65535); FSR.Add(0); } } reader.Close(); configParPtr.Close(); return FSR; } } } return null; }
public static void SaveLimits(string configFile, string limitsFile) { StreamWriter limitsPtr = new StreamWriter(limitsFile); //(@"" + configFile.Substring(0, configFile.LastIndexOf('.')) + "_limits.csv")); //string cfg; // structure as follows==> streamID, parTotal, parameter{parCnt, FSRmin[], FSRmax[]} if (File.Exists(configFile)) { FileStream configFilePtr = new FileStream(configFile, FileMode.Open, FileAccess.Read); XmlTextReader reader = new XmlTextReader(configFilePtr); int pktCnt = 0; limitsPtr.WriteLine("Stream ID, Stream #, Stream Rate, Parameter Number, Parameter Offset, Parameter Occurrences, Parameter Name, Parameter Type [0 - analog 1 - bitvector], Range Maximum, Range Minimum, Limit Maximum, Limit Minimum,X"); string parName = ""; double parOffset = 0; double parOccurr = 1; while (reader.Read()) { if ((reader.IsStartElement()) && (reader.Name == "iNET-X")) { int parCnt = 0; reader.ReadToFollowing("PackageRate"); int pktRate = reader.ReadElementContentAsInt(); reader.ReadToFollowing("StreamID"); int streamID = Convert.ToInt32(reader.ReadElementContentAsString(), 16); reader.ReadToFollowing("Port"); reader.ReadToFollowing("Value"); Console.WriteLine(reader.Value); int port = Convert.ToInt16(reader.ReadElementContentAsString()); MessageBox.Show(String.Format("#{3} Stream ID is: {0} on Port: {2} with rate of {1} Hz\n Press any key to continue..", streamID, pktRate, port, pktCnt)); while (reader.Read()) { //while ((reader.IsStartElement()) && (reader.Name != "iNET-X")) //{ switch (reader.Name) { case "ParameterReference": parName = reader.ReadElementContentAsString(); break; //else if ((reader.IsStartElement()) && (reader.Name == "Offset_Bytes")) case "Offset_Bytes": //reader.ReadToFollowing("Offset_Bytes"); parOffset = reader.ReadElementContentAsDouble(); break; case "Occurrences": parOccurr = reader.ReadElementContentAsDouble(); List<double> FSR = GetParameterFSR(configFile, parName); limitsPtr.Write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{8},{9}\n", streamID, pktCnt, pktRate, parCnt, parOffset, parOccurr, parName, FSR[0], FSR[1], FSR[2]); //Console.WriteLine("Parameter {0}: {1}", parCnt, parName); parCnt++; break; } if (reader.LocalName == "iNET-X") { //Console.WriteLine(reader.LocalName); break; } } //Console.WriteLine("END OF STREAM"); pktCnt++; } } reader.Close(); limitsPtr.Close(); configFilePtr.Close(); } else { MessageBox.Show(String.Format("The file {0} could not be located", configFile)); } MessageBox.Show(String.Format("Configuration file created. \n\n Please open {0} in Excel or other CSV editor.\n Provide your pass/fail criteria for parameters and then click OK afterwards", limitsFile)); }
/// <summary> /// パラメータファイル読込 /// </summary> /// <param name="filePath">パラメータファイルパス</param> public void load(string filePath) { XmlTextReader reader = null; string paramFileFormat = _PARAMETER_FORMAT_000; try { reader = new XmlTextReader(filePath); while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "AMK2BOptions") { break; } if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == "FormatVersion") { paramFileFormat = reader.ReadElementContentAsString(); } if (reader.LocalName == "TotalOptions") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "TotalOptions") { break; } if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == "SizeProportionX") { _fileData.SizeProportionX = reader.ReadElementContentAsDouble(); } if (reader.LocalName == "SizeProportionY") { _fileData.SizeProportionY = reader.ReadElementContentAsDouble(); } if (reader.LocalName == "SizeProportionZ") { _fileData.SizeProportionZ = reader.ReadElementContentAsDouble(); } if (reader.LocalName == "CenterX") { _fileData.CenterX = reader.ReadElementContentAsDouble(); } if (reader.LocalName == "CenterY") { _fileData.CenterY = reader.ReadElementContentAsDouble(); } if (reader.LocalName == "CenterZ") { _fileData.CenterZ = reader.ReadElementContentAsDouble(); } if (reader.LocalName == "Mirror") { _fileData.Mirror = reader.ReadElementContentAsBoolean(); } } } if (reader.LocalName == "CameraOptions") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "CameraOptions") { break; } if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == "KinectElevationAngle") { _fileData.KinectElevationAngle = reader.ReadElementContentAsInt(); } if (reader.LocalName == "ColorDrawEnable") { _fileData.ColorDrawEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "DepthDrawEnable") { _fileData.DepthDrawEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "SkeletonDrawEnable") { _fileData.SkeletonDrawEnable = reader.ReadElementContentAsBoolean(); } } } if (reader.LocalName == "DetailOptions") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "DetailOptions") { break; } if (reader.NodeType != XmlNodeType.Element) { continue; } if (reader.LocalName == "HeadName") { _fileData.HeadName = reader.ReadElementContentAsString(); } if (reader.LocalName == "ShoulderCenterName") { _fileData.ShoulderCenterName = reader.ReadElementContentAsString(); } if (reader.LocalName == "ShoulderRightName") { _fileData.ShoulderRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "ElbowRightName") { _fileData.ElbowRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "WristRightName") { _fileData.WristRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "HandRightName") { _fileData.HandRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "ShoulderLeftName") { _fileData.ShoulderLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "ElbowLeftName") { _fileData.ElbowLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "WristLeftName") { _fileData.WristLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "HandLeftName") { _fileData.HandLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "SpineName") { _fileData.SpineName = reader.ReadElementContentAsString(); } if (reader.LocalName == "HipCenterName") { _fileData.HipCenterName = reader.ReadElementContentAsString(); } if (reader.LocalName == "HipRightName") { _fileData.HipRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "KneeRightName") { _fileData.KneeRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "AnkleRightName") { _fileData.AnkleRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "FootRightName") { _fileData.FootRightName = reader.ReadElementContentAsString(); } if (reader.LocalName == "HipLeftName") { _fileData.HipLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "KneeLeftName") { _fileData.KneeLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "AnkleLeftName") { _fileData.AnkleLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "FootLeftName") { _fileData.FootLeftName = reader.ReadElementContentAsString(); } if (reader.LocalName == "HeadEnable") { _fileData.HeadEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "ShoulderCenterEnable") { _fileData.ShoulderCenterEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "ShoulderRightEnable") { _fileData.ShoulderRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "ElbowRightEnable") { _fileData.ElbowRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "WristRightEnable") { _fileData.WristRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "HandRightEnable") { _fileData.HandRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "ShoulderLeftEnable") { _fileData.ShoulderLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "ElbowLeftEnable") { _fileData.ElbowLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "WristLeftEnable") { _fileData.WristLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "HandLeftEnable") { _fileData.HandLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "SpineEnable") { _fileData.SpineEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "HipCenterEnable") { _fileData.HipCenterEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "HipRightEnable") { _fileData.HipRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "KneeRightEnable") { _fileData.KneeRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "AnkleRightEnable") { _fileData.AnkleRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "FootRightEnable") { _fileData.FootRightEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "HipLeftEnable") { _fileData.HipLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "KneeLeftEnable") { _fileData.KneeLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "AnkleLeftEnable") { _fileData.AnkleLeftEnable = reader.ReadElementContentAsBoolean(); } if (reader.LocalName == "FootLeftEnable") { _fileData.FootLeftEnable = reader.ReadElementContentAsBoolean(); } } } } } catch (Exception) { throw; } finally { if (reader != null) { reader.Close(); } } }