public static ParserFactory <XmlElement> NonPairElement() { return(P.Concat <XmlElement>( P.Literal("<"), XP.ElementName(), XP.AttributeList(), P.Literal("/>") ).Process(p => { var e = new XmlElement( ((Parser <string>)p.Parts[1]).Value, false ); e.Attributes.AddRange( ((Parser <XmlAttribute[]>)p.Parts[2]).Value ); return e; }).Name(nameof(XP.NonPairElement))); }
public static ParserFactory <XmlElement> PairElement() { return(P.Concat <XmlElement>( P.Literal("<"), XP.ElementName(), XP.AttributeList(), P.Literal(">"), P.Repeat <XmlNode[], XmlNode>( XP.Node(), Quantification.Star ).Process(p => p.Matches.Select(m => m.Value).ToArray()), P.Literal("</"), XP.ElementName(), P.Literal(">") ).Process(p => { var e = new XmlElement( ((Parser <string>)p.Parts[1]).Value, true ); e.Attributes.AddRange( ((Parser <XmlAttribute[]>)p.Parts[2]).Value ); e.Content.AddRange( ((Parser <XmlNode[]>)p.Parts[4]).Value ); string ending = ((Parser <string>)p.Parts[6]).Value; if (ending != e.Tag) { throw new XmlParsingException( $"Tag <{ e.Tag }> does not end with the same name, but with </{ ending }> instead." ); } return e; }).Name(nameof(XP.PairElement))); }