/// <summary> /// 加载对象数据 /// </summary> /// <param name="myReader"></param> /// <returns></returns> internal override bool InnerRead(HTMLTextReader myReader) { myItems.Clear(); strText = myReader.ReadToEndTag(this.TagName); if (strText != null) { strText = strText.Replace("<!--", ""); strText = strText.Replace("-->", ""); int index = 0; int index2 = strText.IndexOf("{"); while (index2 >= 0) { int index3 = strText.IndexOf("}", index2); if (index3 > index2) { string strName = strText.Substring(index, index2 - index); string strValue = strText.Substring(index2 + 1, index3 - index2 - 1); HTMLNameStyleItem NewItem = new HTMLNameStyleItem(); NewItem.Name = strName.Trim(); NewItem.CSSString = strValue; myItems.Add(NewItem); } index = index3 + 1; index2 = strText.IndexOf("{", index); } } //myReader.MoveAfter('>'); return(true); }
/// <summary> /// 向XML书写器输出对象数据 /// </summary> /// <remarks>若设置了OwnerDocument.WriteOptions.ScriptWriteCData /// 则脚本代码放置在 CDATA 块中</remarks> /// <param name="myWriter">XML书写器</param> /// <returns>操作是否成功</returns> protected override bool InnerWrite(System.Xml.XmlWriter myWriter) { if (HTMLTextReader.isBlankString(strText)) { myWriter.WriteString(" "); } else { if (myOwnerDocument.WriteOptions.ScriptWriteCData) { string vText = strText.Replace("<![CDATA[", ""); vText = vText.Replace("]]", ""); myWriter.WriteString("//"); myWriter.WriteCData(vText + "//"); } else { string vText = strText + " "; if (vText.IndexOf("--") >= 0) { vText = "因保存需要,将所有的\"--\"转换为 \"@@\"符号\r\n" + vText.Replace("--", "@@"); } myWriter.WriteComment(vText); } //myWriter.WriteString( strText ); } return(true); }
/// <summary> /// 从HTML读取器中加载对象数据 /// </summary> /// <param name="myReader"></param> /// <returns></returns> internal override bool InnerRead(HTMLTextReader myReader) { strLoadErrorMsg = null; strSourceXML = myReader.ReadToEndTag(this.TagName); try { myXMLDocument.RemoveAll(); System.Xml.XmlNamespaceManager nsm = new System.Xml.XmlNamespaceManager(myXMLDocument.NameTable); foreach (HTMLAttribute attr in myOwnerDocument.Attributes) { string vName = attr.Name; if (vName.ToLower().StartsWith(StringConstAttributeName.XMLNS)) { int index = vName.IndexOf(":"); if (index > 0) { string NsName = vName.Substring(index + 1); nsm.AddNamespace(NsName, attr.Value); } } } System.Xml.XmlParserContext pc = new System.Xml.XmlParserContext(myXMLDocument.NameTable, nsm, null, System.Xml.XmlSpace.None); System.Xml.XmlTextReader myXMLReader = new System.Xml.XmlTextReader(strSourceXML, System.Xml.XmlNodeType.Element, pc); myXMLDocument.Load(myXMLReader); myXMLReader.Close(); } catch (Exception ext) { myXMLDocument.RemoveAll(); strLoadErrorMsg = "加载XML数据岛信息错误 - " + ext.Message; } return(true); }
/// <summary> /// 加载指定的HTML字符串 /// </summary> /// <param name="strHTML">HTML字符串</param> /// <returns>操作是否成功</returns> public bool LoadHTML(string strHTML) { bolLoadingFlag = true; HTMLTextReader TxtReader = new HTMLTextReader(strHTML); bool bolResult = Read(TxtReader); bolLoadingFlag = false; return(bolResult); }
internal override bool MeetEndTag(HTMLTextReader myReader, string vTagName) { if (vTagName == StringConstTagName.Table || vTagName == StringConstTagName.TBody) { myReader.MovePreTo('<'); return(true); } return(base.MeetEndTag(myReader, vTagName)); }
/// <summary> /// 读取元素所有的属性值 /// </summary> /// <param name="myReader">HTML文本读取器</param> internal virtual void ReadAttribute(HTMLTextReader myReader) { string strName = null; string strValue = null; while (!myReader.EOF) { myReader.SkipWhiteSpace(); // 若遇到元素结束标记则退出 if (myReader.StartWidth("/>")) { myReader.MoveStep(2); break; } if (myReader.Peek == '>' || myReader.Peek == '<') { myReader.MoveNext(); break; } // 读取属性名 strName = myReader.ReadWord( ); if (strName == null) { break; } strName = strName.ToLower(); // 读取属性值 myReader.SkipWhiteSpace(); if (myReader.EOF || myReader.Peek != '=') { strValue = "1"; } else { myReader.MoveNext(); strValue = myReader.ReadQuotMarkText(); } // 保存属性 if (System.Xml.XmlReader.IsName(strName)) { this.SetAttribute(strName, strValue); } } //while // 设置样式控制表属性 if (HasAttribute(StringConstAttributeName.Style)) { myStyle = new HTMLStyle(); myStyle.CSSString = GetAttribute(StringConstAttributeName.Style); } else { myStyle = null; } }
/// <summary> /// 遇到结束标签 /// </summary> /// <param name="myReader">HTML文本读取器</param> /// <param name="vTagName">当前标签名称</param> /// <returns></returns> internal override bool MeetEndTag(HTMLTextReader myReader, string vTagName) { if (myParent != null && vTagName == myParent.TagName) { return(true); } else { return(false); } }
/// <summary> /// 从一个文本流中加载对象数据 /// </summary> /// <param name="myReader">HTML文本读取器</param> /// <returns>操作是否成功</returns> internal virtual bool Read(HTMLTextReader myReader) { ReadAttribute(myReader); if (!myReader.EOF) { return(InnerRead(myReader)); } else { return(false); } }
/// <summary> /// 解析HTML时遇到结束标签时的处理 /// </summary> /// <param name="myReader"></param> /// <param name="vTagName"></param> /// <returns></returns> internal virtual bool MeetEndTag(HTMLTextReader myReader, string vTagName) { if (vTagName == this.TagName || this.CheckChildTagName(vTagName) == false) { myReader.MoveAfter('>'); return(true); } else { return(false); } }
/// <summary> /// 处理结束标签 /// </summary> /// <param name="myReader"></param> /// <param name="vTagName"></param> /// <returns></returns> internal override bool MeetEndTag(HTMLTextReader myReader, string vTagName) { if (vTagName == StringConstTagName.Head) { myReader.MoveAfter('>'); return(true); } else { return(false); } }
/// <summary> /// 加载内容 /// </summary> /// <param name="myReader">HTML文本读取器</param> /// <returns>操作是否成功</returns> internal virtual bool InnerRead(HTMLTextReader myReader) { if (this.HasText) { // 加载文本内容 string vText = myReader.ReadString(); if (myReader.StartWidth("</" + this.TagName)) { myReader.MoveAfter('>'); } this.Text = vText; } return(true); }
// // /// <summary> // /// 从指定的URL使用指定的编码格式加载文档 // /// </summary> // /// <param name="strUrl">URL地址</param> // /// <param name="encoding">文本编码格式</param> // /// <returns>操作是否成功</returns> // public bool LoadUrl( string strUrl , System.Text.Encoding encoding ) // { // myAttributes.Clear(); // myChildNodes.Clear(); // string strHTML = null; // System.Net.HttpWebRequest myReq = ( System.Net.HttpWebRequest ) System.Net.WebRequest.Create( strUrl ); // myReq.Method = "GET" ; // System.Net.WebResponse myResp = myReq.GetResponse(); // System.IO.Stream myStream = myResp.GetResponseStream(); // System.IO.MemoryStream myOutStream = new System.IO.MemoryStream(); // byte[] bs = new byte[ 512 ]; // while( true ) // { // int len = myStream.Read( bs , 0 , bs.Length ); // if( len == 0 ) // break; // myOutStream.Write( bs , 0 , len ); // } // myStream.Close(); // myResp.Close(); // myReq.Abort(); // // bs = myOutStream.ToArray(); // myOutStream.Close(); // if( bs == null && bs.Length == 0 ) // return false; // strHTML = encoding.GetString( bs ); // return this.LoadHTML( strHTML ); // } /// <summary> /// 加载指定的HTML文档 /// </summary> /// <param name="strFile">HTML文件名</param> /// <returns>操作是否成功</returns> public bool Load(string strFile) { bolLoadingFlag = true; System.IO.StreamReader myReader = new System.IO.StreamReader(strFile, System.Text.Encoding.Default); string strText = myReader.ReadToEnd(); myReader.Close(); HTMLTextReader TxtReader = new HTMLTextReader(strText); bool bolResult = Read(TxtReader); bolLoadingFlag = false; strLocation = strFile; return(bolResult); }
/// <summary> /// 内部的输出对象数据到XML书写器 /// </summary> /// <param name="myWriter">XML书写器</param> /// <returns>操作是否成功</returns> protected override bool InnerWrite(System.Xml.XmlWriter myWriter) { if (myOwnerDocument.WriteOptions.TextAreaOutputXSL) { myWriter.WriteStartElement(StringConstXSLT.Value_of); myWriter.WriteAttributeString(StringConstXSLT.Value_of, this.Name); myWriter.WriteEndElement(); } else { myWriter.WriteString(HTMLTextReader.isBlankString(strText) ? " " : strText); } return(true); }
internal override bool Read(HTMLTextReader myReader) { if (base.Read(myReader)) { if (this.HasAttribute("charset")) { string v = this.GetAttribute("charset"); if (string.IsNullOrEmpty(this.OwnerDocument.CharSet) == false) { if (string.Compare(v, this.OwnerDocument.CharSet, true) != 0) { this.OwnerDocument.CharSet = v; throw new HTMLChangeCharSetException(v); } } } if (this.HasAttribute("content")) { System.Collections.Specialized.NameValueCollection myValues = HTMLTextReader.AnalyseValueString(this.GetAttribute("content")); foreach (string strKey in myValues.Keys) { if (string.Compare(strKey, "charset", true) == 0) { string strValue = myValues[strKey]; if (strValue != null) { strValue = strValue.Trim(); if (myOwnerDocument.CharSet != null) { if (string.Compare(strValue, myOwnerDocument.CharSet, true) != 0) { myOwnerDocument.CharSet = strValue; throw new HTMLChangeCharSetException(strValue); } } } } } } return(true); } else { return(false); } }
/// <summary> /// 读取HTML代码,忽略HTML注释 /// </summary> /// <param name="myReader">HTML文本读取器</param> /// <returns>操作是否成功</returns> internal override bool InnerRead(HTMLTextReader myReader) { strText = myReader.ReadToEndTag(this.TagName); if (strText != null) { int index = strText.IndexOf("<!--"); if (index >= 0) { strText = strText.Substring(index + 4); } index = strText.LastIndexOf("-->"); if (index >= 0) { strText = strText.Substring(0, index); } } //myReader.MoveAfter('>'); return(true); }
/// <summary> /// 已重载:从HTML文本读取器读取数据加载对象 /// </summary> /// <param name="myReader"></param> /// <returns></returns> internal override bool Read(HTMLTextReader myReader) { myAttributes.Clear(); myChildNodes.Clear(); HTMLElement element = new HTMLHeadElement(); element.Parent = this; element.OwnerDocument = this; myChildNodes.Add(element); element = new HTMLBodyElement(); element.Parent = this; element.OwnerDocument = this; myChildNodes.Add(element); return(base.InnerRead(myReader)); // // myReader.ReadUntil("<html"); // myReader.MoveStep(5); // return base.Read( myReader ); }
/// <summary> /// 输出对象数据到一个XML书写器中 /// </summary> /// <remarks> /// 若设置了OwnerDocument.WriteOptions.WriteWhitespace则输出两边的空白字符,否则不会输出两边的空白字符 /// 若设置了OwnerDocument.WriteOptions.NormalizeSpace则对输出的字符进行 normalizespace 处理 /// 若设置了OwnerDocument.WriteOptions.AddSpan且父标签还有其他元素则在外套上 span 标签 /// 若设置了OwnerDocument.WriteOptions.TextOutPutXSL 则将所有限定标记组合(默认为"[]")包围的部分进行XSL输出 /// OwnerDocument.WriteOptions.TextFieldPrefix和TextFieldEndfix指定了限定标记组合 /// 例如 "aaa[bbb]ccc" 输出为 "aaa<xsl:vlaue-of select="bbb" />ccc" /// </remarks> /// <param name="myWriter">XML书写器</param> /// <returns>操作是否成功</returns> public override bool Write(System.Xml.XmlWriter myWriter) { if (HTMLTextReader.HasContent(strText)) { string vText; if (myOwnerDocument.WriteOptions.WriteWhitespace) { vText = strText; } else { vText = strText.Trim(); } if (myOwnerDocument.WriteOptions.NormalizeSpace) { vText = HTMLTextReader.NormalizeSpace(vText); } bool bolAddSpan = (myOwnerDocument.WriteOptions.AddSpan && (myParent == null || myParent.ChildNodes.Count != 1)); if (myOwnerDocument.WriteOptions.TextOutPutXSL) { string[] strItems = HTMLTextReader.SplitVariableString(vText, myOwnerDocument.WriteOptions.TextFieldPrefix, myOwnerDocument.WriteOptions.TextFieldEndfix); if (bolAddSpan) { myWriter.WriteStartElement(StringConstTagName.Span); } for (int iCount = 0; iCount < strItems.Length; iCount++) { string strItem = strItems[iCount]; if (strItem != null) { if ((iCount % 2) == 1 && strItem != "") { if (strItem.IndexOf("\r\n") >= 0) { strItem = strItem.Replace("\r\n", ""); } if (strItem.StartsWith("num:")) { strItem = strItem.Substring(4); myWriter.WriteStartElement(StringConstXSLT.IF); myWriter.WriteAttributeString(StringConstXSLT.Test, strItem + "!='0.0000'"); myWriter.WriteStartElement(StringConstXSLT.Value_of); myWriter.WriteAttributeString(StringConstXSLT.Select, myOwnerDocument.WriteOptions.FixXSLSource(strItem)); myWriter.WriteEndElement(); myWriter.WriteEndElement(); } else if (strItem.StartsWith("%:")) { strItem = strItem.Substring(2); myWriter.WriteStartElement(StringConstXSLT.Value_of); myWriter.WriteAttributeString(StringConstXSLT.Select, "format-number(" + myOwnerDocument.WriteOptions.FixXSLSource(strItem) + " , '0.00')"); myWriter.WriteEndElement(); } else if (strItem.StartsWith("call:")) { strItem = strItem.Substring(5); myWriter.WriteStartElement(StringConstXSLT.Calltemplate); myWriter.WriteAttributeString(StringConstXSLT.Name, strItem); myWriter.WriteEndElement(); } else { myWriter.WriteStartElement(StringConstXSLT.Value_of); myWriter.WriteAttributeString(StringConstXSLT.Select, myOwnerDocument.WriteOptions.FixXSLSource(strItem)); myWriter.WriteEndElement(); } } else { int index = 0; int index2 = strItem.IndexOf(StringConstEntity.WhiteSpace); if (index2 >= 0) { while (index2 >= 0) { if (index2 > index) { myWriter.WriteString(strItem.Substring(index, index2 - index)); } myWriter.WriteStartElement(StringConstXSLT.Text); myWriter.WriteAttributeString(StringConstXSLT.Disable_output_escaping, StringConstValue._Yes); myWriter.WriteString(StringConstEntity.WhiteSpace); myWriter.WriteEndElement(); index = index2 + 6; index2 = strItem.IndexOf(StringConstEntity.WhiteSpace, index); } if (index < strItem.Length) { myWriter.WriteString(strItem.Substring(index)); } } else { myWriter.WriteString(strItem); } } } } //for if (bolAddSpan) { myWriter.WriteEndElement(); } } else { if (bolAddSpan) { myWriter.WriteElementString(StringConstTagName.Span, vText); } else { myWriter.WriteString(vText); } } } return(true); }
internal override bool Read(HTMLTextReader myReader) { strText = myReader.ReadString(); return(true); }
/// <summary> /// 从HTML文本读取器读取数据并加载子元素 /// </summary> /// <param name="myReader">HTML文本读取器</param> /// <returns>操作是否成功</returns> internal override bool InnerRead(HTMLTextReader myReader) { string strTagName; while (!myReader.EOF) { string strText = myReader.ReadString(); if (strText != null && this.CheckChildTagName(StringConstTagName.TextNode)) { HTMLTextNodeElement txt = new HTMLTextNodeElement(); txt.Text = strText; AppendChild(txt); } if (myReader.EOF) { break; } if (myReader.NextChar == '!') { if (myReader.StartWidth("<!--")) { myReader.MoveStep(4); strText = myReader.ReadUntil("-->"); if (strText != null) { if (this.CheckChildTagName(StringConstTagName.Comment)) { HTMLCommentElement com = new HTMLCommentElement(); com.Text = strText; AppendChild(com); } } myReader.MoveStep(3); continue; } else { myReader.MoveAfter('>'); myReader.SkipWhiteSpace(); continue; } } // else if( myReader.NextChar == '/') // { // if( myReader.StartWidth("</" + this.TagName )) // { // myReader.MoveAfter('>'); // break; // } // else // { // myReader.MoveAfter('>'); // continue; // } // } else if (myReader.NextChar == '/') { myReader.MoveStep(2); string vEndTag = myReader.PeekWord(); myReader.MoveStep(-2); // if( ( vEndTag != null ) && vEndTag.ToLower() == this.TagName ) // { // myReader.MoveAfter('>'); // break; // } if ((vEndTag != null) && (MeetEndTag(myReader, vEndTag.ToLower()))) { break; } else { myReader.MoveAfter('>'); continue; } } else if (myReader.NextChar == '?') { myReader.MoveAfter('>'); continue; } myReader.MoveNext(); strTagName = myReader.ReadWord(); if (strTagName != null) { strTagName = strTagName.ToLower(); if (!BeforeLoadTag(strTagName)) { myReader.MovePreTo('<'); break; } if (this.CheckChildTagName(strTagName)) { HTMLElement NewElement = myOwnerDocument.InnerCreateElement(strTagName, this); if (NewElement != null) { AppendChild(NewElement); NewElement.Read(myReader); } else { myReader.ReadToEndTag(strTagName); } } } } //while return(true); }
/// <summary> /// 创建元素对象实例 /// </summary> /// <param name="TagName">HTML标签名称</param> /// <param name="vParent">创建的元素的父节点</param> /// <returns>创建的HTML元素对象</returns> internal HTMLElement InnerCreateElement(string TagName, HTMLContainer vParent) { if (HTMLTextReader.isBlankString(TagName)) { return(null); } HTMLElement NewElement = null; TagName = TagName.ToLower().Trim(); switch (TagName) { case StringConstTagName.HTML: if (vParent == null || vParent == this) { NewElement = this; } else { NewElement = new HTMLDivElement(); } break; case StringConstTagName.Head: if (vParent is HTMLDocument) { NewElement = this.Head; if (NewElement == null) { NewElement = new HTMLHeadElement(); } } break; case StringConstTagName.Body: if (vParent is HTMLDocument) { NewElement = this.Body; if (NewElement == null) { NewElement = new HTMLBodyElement(); } } else { NewElement = new HTMLDivElement(); } break; case StringConstTagName.IEDevicerect: NewElement = new HTMLIEDeviceRect(); break; case StringConstTagName.IEHeaderFooter: NewElement = new HTMLIEHeaderfooter(); break; case StringConstTagName.IELayoutrect: NewElement = new HTMLIELayoutrect(); break; case StringConstTagName.IETemplatePrinter: NewElement = new HTMLIETemplateprinter(); break; case StringConstTagName.H1: NewElement = new HTMLHnElement(); ((HTMLHnElement)NewElement).Level = 1; break; case StringConstTagName.H2: NewElement = new HTMLHnElement(); ((HTMLHnElement)NewElement).Level = 2; break; case StringConstTagName.H3: NewElement = new HTMLHnElement(); ((HTMLHnElement)NewElement).Level = 3; break; case StringConstTagName.H4: NewElement = new HTMLHnElement(); ((HTMLHnElement)NewElement).Level = 4; break; case StringConstTagName.H5: NewElement = new HTMLHnElement(); ((HTMLHnElement)NewElement).Level = 5; break; case StringConstTagName.H6: NewElement = new HTMLHnElement(); ((HTMLHnElement)NewElement).Level = 6; break; case StringConstTagName.Input: NewElement = new HTMLInputElement(); break; case StringConstTagName.TextArea: NewElement = new HTMLTextAreaElement(); break; case StringConstTagName.Select: NewElement = new HTMLSelectElement(); break; case StringConstTagName.Option: NewElement = new HTMLOptionElement(); break; case StringConstTagName.Form: NewElement = new HTMLFormElement(); break; case StringConstTagName.A: NewElement = new HTMLAElement(); break; case StringConstTagName.B: NewElement = new HTMLBElement(); break; case StringConstTagName.Pre: NewElement = new HTMLPreElement(); break; case StringConstTagName.Span: NewElement = new HTMLSpanElement(); break; case StringConstTagName.Div: NewElement = new HTMLDivElement(); break; case StringConstTagName.P: NewElement = new HTMLPElement(); break; case StringConstTagName.Br: NewElement = new HTMLBRElement(); break; case StringConstTagName.Applet: NewElement = new HTMLAppletElement(); break; case StringConstTagName.TextNode: NewElement = new HTMLTextNodeElement(); break; case StringConstTagName.Object: NewElement = new HTMLObjectElement(); break; case StringConstTagName.Script: NewElement = new HTMLScriptElement(); break; case StringConstTagName.Link: NewElement = new HTMLLinkElement(); break; case StringConstTagName.Font: NewElement = new HTMLFontElement(); break; case StringConstTagName.Meta: NewElement = new HTMLMetaElement(); break; case StringConstTagName.BGSound: NewElement = new HTMLBGSoundElement(); break; case StringConstTagName.Param: NewElement = new HTMLParamElement(); break; case StringConstTagName.Comment: NewElement = new HTMLCommentElement(); break; case StringConstTagName.Hr: NewElement = new HTMLHRElement(); break; case StringConstTagName.Table: NewElement = new HTMLTableElement(); break; case StringConstTagName.TBody: NewElement = new HTMLTBodyElement(); break; case StringConstTagName.Tr: NewElement = new HTMLTRElement(); break; case StringConstTagName.Td: NewElement = new HTMLTDElement(); break; case StringConstTagName.Col: NewElement = new HTMLColElement(); break; case StringConstTagName.Style: NewElement = new HTMLStyleElement(); break; case StringConstTagName.Img: NewElement = new HTMLImgElement(); break; case StringConstTagName.Title: NewElement = new HTMLTitleElement(); break; case StringConstTagName.UL: NewElement = new HTMLULElement(); break; case StringConstTagName.LI: NewElement = new HTMLLIElement(); break; case StringConstTagName.Map: NewElement = new HTMLMapElement(); break; case StringConstTagName.Area: NewElement = new HTMLAreaElement(); break; case StringConstTagName.HTAApplication: NewElement = new HTMLHTAApplicationElement(); break; case StringConstTagName.FrameSet: NewElement = new HTMLFrameSetElement(); break; case StringConstTagName.Frame: NewElement = new HTMLFrameElement(); break; case StringConstTagName.Label: NewElement = new HTMLLabelElement(); break; case StringConstTagName.Marquee: NewElement = new HTMLMarqueeElement(); break; case StringConstTagName.IFrame: NewElement = new HTMLIFrameElement(); break; case StringConstTagName.XML: NewElement = new HTMLXMLElement(); break; case StringConstTagName.Sup: NewElement = new HTMLSupElement(); break; case StringConstTagName.Sub: NewElement = new HTMLSubElement(); break; case StringConstTagName.Nobr: NewElement = new HTMLNobarElement(); break; default: NewElement = new HTMLUnknowElement(); (( HTMLUnknowElement)NewElement).SetTagName(TagName); break; } if (NewElement != null) { NewElement.OwnerDocument = this; NewElement.Parent = vParent; } return(NewElement); }
internal override bool InnerRead(HTMLTextReader myReader) { strText = myReader.ReadToEndTag(this.TagName); return(true); }