MoveToAttribute() public méthode

Moves to the atttribute with the specified Name.
public MoveToAttribute ( string name ) : bool
name string The qualified name of the attribute.
Résultat bool
Exemple #1
0
        void Debug(SgmlReader sr)
        {
            NodeTypeFlags[] AllowedContentMap = new NodeTypeFlags[19] {
                                                                          NodeTypeFlags.None, // none
                                                                          NodeTypeFlags.Element | NodeTypeFlags.Attribute | NodeTypeFlags.Text | NodeTypeFlags.CDATA | NodeTypeFlags.EntityReference | NodeTypeFlags.ProcessingInstruction | NodeTypeFlags.Comment | NodeTypeFlags.Whitespace | NodeTypeFlags.SignificantWhitespace | NodeTypeFlags.EndElement, // element
                                                                          NodeTypeFlags.Text | NodeTypeFlags.EntityReference, // attribute
                                                                          NodeTypeFlags.None, // text
                                                                          NodeTypeFlags.None, // cdata
                                                                          NodeTypeFlags.None, // entity reference
                                                                          NodeTypeFlags.None, // entity
                                                                          NodeTypeFlags.None, // processing instruction
                                                                          NodeTypeFlags.None, // comment
                                                                          NodeTypeFlags.Comment | NodeTypeFlags.DocumentType | NodeTypeFlags.Element | NodeTypeFlags.EndElement | NodeTypeFlags.ProcessingInstruction | NodeTypeFlags.Whitespace | NodeTypeFlags.SignificantWhitespace | NodeTypeFlags.XmlDeclaration, // document
                                                                          NodeTypeFlags.None, // document type
                                                                          NodeTypeFlags.None, // document fragment (not expecting these)
                                                                          NodeTypeFlags.None, // notation
                                                                          NodeTypeFlags.None, // whitespace
                                                                          NodeTypeFlags.None, // signification whitespace
                                                                          NodeTypeFlags.None, // end element
                                                                          NodeTypeFlags.None, // end entity
                                                                          NodeTypeFlags.None, // filler
                                                                          NodeTypeFlags.None, // xml declaration.
            };

            Stack s = new Stack();

            while (sr.Read()) {
                if (sr.NodeType == XmlNodeType.EndElement) {
                    s.Pop();
                }
                if (s.Count > 0) {
                    XmlNodeType pt = (XmlNodeType)s.Peek();
                    NodeTypeFlags p = NodeTypeMap[(int)pt];
                    NodeTypeFlags f = NodeTypeMap[(int)sr.NodeType];
                    if ((AllowedContentMap[(int)pt]& f) != f) {
                        Console.WriteLine("Invalid content!!");
                    }
                }
                if (s.Count != sr.Depth-1) {
                    Console.WriteLine("Depth is wrong!");
                }
                if ( (sr.NodeType == XmlNodeType.Element && !sr.IsEmptyElement) ||
                    sr.NodeType == XmlNodeType.Document) {
                    s.Push(sr.NodeType);
                }

                for (int i = 1; i < sr.Depth; i++)
                    Console.Write("  ");
                Console.Write(sr.NodeType.ToString() + " " + sr.Name);
                if (sr.NodeType == XmlNodeType.Element && sr.AttributeCount > 0) {
                    sr.MoveToAttribute(0);
                    Console.Write(" (" + sr.Name+"="+sr.Value + ")");
                    sr.MoveToElement();
                }
                if (sr.Value != null) {
                    Console.Write(" " + sr.Value.Replace("\n"," ").Replace("\r",""));
                }
                Console.WriteLine();
            }
        }
Exemple #2
0
		private static string ConvertCommentToMarkdown(string body)
		{
			var sb = new StringBuilder();

			var sgmlReader = new SgmlReader
				{
					InputStream = new StringReader(body),
					DocType = "HTML",
					WhitespaceHandling = WhitespaceHandling.Significant,
					CaseFolding = CaseFolding.ToLower
				};

			bool outputEndElement = false;
			int indentLevel = 0;
			while (sgmlReader.Read())
			{
				switch (sgmlReader.NodeType)
				{
					case XmlNodeType.Text:
						if (indentLevel > 0)
							sb.Append("\t");
						sb.AppendLine(sgmlReader.Value);
						break;
					case XmlNodeType.Element:
						switch (sgmlReader.LocalName)
						{
							case "h1":
								sb.Append("## ");
								break;
							case "br":
								sb.AppendLine("  ");
								break;
							case "a":
								if (sgmlReader.MoveToAttribute("href"))
								{
									string url = sgmlReader.Value;
									sgmlReader.Read();

									sb.AppendFormat("[{0}]({1})", sgmlReader.Value, url);
								}
								break;
							case "html":
								break;
							case "strong":
							case "b":
								sb.AppendFormat("**{0}**", sgmlReader.Value);
								break;
							case "i":
							case "em":
								sb.AppendFormat("_{0}_", sgmlReader.Value);
								break;
							case "li":
								sb.AppendFormat("- {0}", sgmlReader.Value);
								break;
							case "pre":
							case "code":
							case "quote":
								indentLevel = 1;
								break;
							case "ul":
							case "ol":
							case "img":
								break;
							default:
								outputEndElement = true;
								sb.Append("<").Append(sgmlReader.LocalName);
								break;
						}
						break;
					case XmlNodeType.SignificantWhitespace:
					case XmlNodeType.Whitespace:
					case XmlNodeType.CDATA:
						break;
					case XmlNodeType.EndElement:
						indentLevel = 0;
						if (outputEndElement)
							sb.Append(">");
						outputEndElement = false;
						break;
					default:
						throw new ArgumentOutOfRangeException();
				}
			}

			return sb.ToString();
		}