/// <summary> /// Example above: if "A", get 0,1; if "B" get 2 /// </summary> public List <Hss_XML_obj> Get_obj_list(params string[] name_arr) { Hss_XML_obj curr_obj = this; for (int i = 0; i < name_arr.Length - 1; ++i) { string name = name_arr[i]; if (curr_obj.XMLobj_dic.ContainsKey(name)) { curr_obj = curr_obj.XMLobj_dic[name][0]; } else { return(new List <Hss_XML_obj>()); } } if (name_arr.Length > 0) { string name = name_arr[name_arr.Length - 1]; if (curr_obj.XMLobj_dic.ContainsKey(name)) { return(curr_obj.XMLobj_dic[name]); } } return(new List <Hss_XML_obj>()); }
/// <summary> /// Depth first search for an XML_obj /// </summary> public Hss_XML_obj Search_obj(string name) { if (string.IsNullOrEmpty(name)) { return(null); } if (this.XMLobj_dic.ContainsKey(name)) //if found return the first one { List <Hss_XML_obj> list = this.XMLobj_dic[name]; if (list.Count > 0) { return(list[0]); } } foreach (List <Hss_XML_obj> list in this.XMLobj_dic.Values) { foreach (Hss_XML_obj xo in list) { Hss_XML_obj res = xo.Search_obj(name); if (res != null) { return(res); } } } return(null); }
private void Parse_InElement() { while (this.ReadNext_char()) { if (this.blankChars_hs.Contains(this.currChar)) { continue; } else if (this.currChar == '>') { this.ParsingState = XML_ParseState.OutElement; } else if (this.currChar == '/' || this.currChar == '?') { Hss_XML_obj xo = this.element_st.Pop(); this.element_st.Peek().Add_obj(xo); //xo.Show();//test output this.ParsingState = XML_ParseState.OutElement; } else { this.sb.Clear(); this.sb.Append(this.currChar); this.ParsingState = XML_ParseState.AttrName; } break; } }
private void Parse_CommentValue() { char c1 = ' ', c2 = ' '; while (this.ReadNext_char()) { if (this.currChar == '>') { if (c1 == '-' && c2 == '-') { string comment = sb.ToString(); if (comment.Length >= 4) { comment = comment.Substring(2, comment.Length - 4); } Hss_XML_obj xo = new Hss_XML_obj("$Comment$", XML_ElementType.Comment); xo.value = comment; this.element_st.Peek().Add_obj(xo); this.sb.Clear(); this.ParsingState = XML_ParseState.OutElement; break; } } c1 = c2; c2 = this.currChar; this.sb.Append(this.currChar); } }
private void Parse_AttrName() { while (this.ReadNext_char()) { if (this.blankChars_hs.Contains(this.currChar) || this.currChar == '=') { Hss_XML_obj xo = this.element_st.Peek(); xo.SetTempKey(this.sb.ToString()); this.sb.Clear(); this.ParsingState = XML_ParseState.AfterAttrName; break; } else if (this.currChar == '>') { this.sb.Clear(); this.ParsingState = XML_ParseState.OutElement; break; } else { this.sb.Append(this.currChar); } } }
public static DateTime Get_datetime(Hss_XML_obj xo, string format, DateTime defaultValue) { if (xo == null) { return(defaultValue); } string strVal = xo.value; DateTime val = DateTime.MinValue; if (string.IsNullOrEmpty(format)) { if (DateTime.TryParse(strVal, out val)) { return(val); } } else { if (DateTime.TryParseExact(strVal, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out val)) { return(val); } } return(defaultValue); }
/**********************************Add and Find XML objects************************************************/ public void Add_obj(Hss_XML_obj obj) { if (obj == null) { return; } if (!this.XMLobj_dic.ContainsKey(obj.name))//add to dictionary { List <Hss_XML_obj> list = new List <Hss_XML_obj>(); this.XMLobj_dic[obj.name] = list; } this.XMLobj_dic[obj.name].Add(obj); }
private void Parse_ElementStart() { if (!this.ReadNext_char()) { return; } if (this.currChar == '?') { this.sb.Clear(); this.ParsingState = XML_ParseState.ElementName; this.ElementType = XML_ElementType.Declare; } else if (this.currChar == '!') { this.sb.Clear(); this.ParsingState = XML_ParseState.CommentValue; this.ElementType = XML_ElementType.Comment; } else if (this.currChar == '>') { Hss_XML_obj xo = this.element_st.Peek(); xo.Add_obj(new Hss_XML_obj(null, this.ElementType)); this.ParsingState = XML_ParseState.OutElement; } else if (this.currChar == '/') { Hss_XML_obj xo = this.element_st.Pop(); xo.value = HssStr.SafeXML_to_ori(this.sb.ToString().Trim()); this.sb.Clear(); this.element_st.Peek().Add_obj(xo); //xo.Show();//test output this.ParsingState = XML_ParseState.ElementEnd; } else { this.sb.Clear(); this.sb.Append(this.currChar); this.ParsingState = XML_ParseState.ElementName; this.ElementType = XML_ElementType.Normal; } }
public static int Get_int(Hss_XML_obj xo, int defaultValue = 0) { if (xo == null) { return(defaultValue); } string strVal = xo.value; int val = defaultValue; if (int.TryParse(strVal, out val)) { return(val); } else { return(defaultValue); } }
public static bool Get_bool(Hss_XML_obj xo) { if (xo == null) { return(false); } string strVal = xo.value; if (string.IsNullOrEmpty(strVal)) { return(false); } else { strVal = strVal.Trim(); return(strVal.Equals("True", StringComparison.OrdinalIgnoreCase)); } }
public static decimal Get_decimal(Hss_XML_obj xo, decimal defaultValue = 0) { if (xo == null) { return(defaultValue); } string strVal = xo.value; decimal val = defaultValue; if (decimal.TryParse(strVal, out val)) { return(val); } else { return(defaultValue); } }
private void Parse_AttrValue() { while (this.ReadNext_char()) { if ((this.quoteChars_hs.Contains(this.currChar) && this.value_hasQuote_flag) || (this.blankChars_hs.Contains(this.currChar) && !this.value_hasQuote_flag)) { Hss_XML_obj xo = this.element_st.Peek(); xo.SetTempValue(this.sb.ToString()); this.sb.Clear(); this.ParsingState = XML_ParseState.InElement; break; } else { this.sb.Append(this.currChar); } } }