4

Resolved

OptionOutputOriginalCase is ignored for attributes when not outputting XML

description

I've been using Html Agility Pack to automatically modify some HTML forms and insert ASP.NET tags into them. I ran into a problem whereby I could specify attributes with names such as "AssociatedControlID", but they would always be output as "associatedcontrolid", even when OptionOutputOriginalCase was set to true. I had a look in the source for the library and the problem appears to be in HtmlNode.WriteAttribute. The check for OptionOutputOriginalCase = true is only carried out if OptionOutputAsXml is also true. I think this option should be checked in either case, and if it is true, the original attribute name used. Unfortunately, I can't switch on XML output, as this causes problems with ASP.NET tag names like "asp:TextBox", so I'm using a modified version of the library to get around the problem for now.

comments

Amtiskaw wrote Nov 9, 2009 at 6:45 PM

I will submit a patch for this myself, if I get time.

xjpmauricio wrote Jan 24, 2012 at 12:12 PM

Hi,

i've been having the same problem you described.

Can you post your solution here?

Thanks.

xjpmauricio wrote Jan 26, 2012 at 12:26 PM

Hi, i solved my problem this way:

Find file "HtmlNode.cs".

Change the method "WriteAttribute" to:

internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
    {
        string name;
        string quote = att.QuoteType == AttributeValueQuote.DoubleQuote ? "\"" : "'";
        if (_ownerdocument.OptionOutputAsXml)
        {
            if (_ownerdocument.OptionOutputUpperCase)
            {
                name = att.XmlName.ToUpper();
            }
            else
            {
                name = att.XmlName;
            }
            if (_ownerdocument.OptionOutputOriginalCase)
                name = att.OriginalName;

            outText.Write(" " + name + "=" + quote + HtmlDocument.HtmlEncode(att.XmlValue) + quote);
        }
        else
        {
            if (_ownerdocument.OptionOutputUpperCase)
            {
                name = att.Name.ToUpper();
            }
            else
            {
                if (_ownerdocument.OptionOutputOriginalCase)
                {
                    name = att.OriginalName;
                }
                else
                {
                    name = att.Name;
                }
            }

            if (att.Name.Length >= 4)
            {
                if ((att.Name[0] == '<') && (att.Name[1] == '%') &&
                    (att.Name[att.Name.Length - 1] == '>') && (att.Name[att.Name.Length - 2] == '%'))
                {
                    outText.Write(" " + name);
                    return;
                }
            }
            if (_ownerdocument.OptionOutputOptimizeAttributeValues)
            {
                if (att.Value.IndexOfAny(new Char[] {(char) 10, (char) 13, (char) 9, ' '}) < 0)
                {
                    outText.Write(" " + name + "=" + att.Value);
                }
                else
                {
                    outText.Write(" " + name + "=" + quote + att.Value + quote);
                }
            }
            else
            {
                outText.Write(" " + name + "=" + quote + att.Value + quote);
            }
        }
    }

obara88 wrote Jun 22, 2012 at 12:44 PM

Any fix ?

rburte wrote Oct 2, 2012 at 9:27 PM

I submitted a patch for this issue, 12834, but screwed up the description a little.

DarthObiwan wrote Oct 10, 2012 at 12:12 AM

This has been fixed in source control and will be in the next release
http://htmlagilitypack.codeplex.com/SourceControl/changeset/99962

arsh wrote Jan 29, 2013 at 4:10 AM

Can any help me how to avoid case sensitivity while selecting nodes?

rburte wrote Jun 14, 2013 at 5:35 PM

Arsh: it's not related to this issue at all. Sounds like you should open a discussion about this.
The short answer is that selecting nodes uses XSLT. If you can search XSLT in a case insensitive way, you can use that solution, perhaps this?
http://support.microsoft.com/kb/315719

CeciliaLam wrote Mar 10 at 5:46 PM

When would this (v1.4.7) be release?