0

I have a HTML file, with several input fields some with an id, loaded into a C# string:

<div>
   <input id="inpName" type="text" />
   <input type="checkbox" />
</div>

Lets say I want to add an required attribute to the input with the id inpName.

In jQuery I would do:

$('input#inpName').prop('required', true);

Q: How can I achieve add this attribute without adding the HTMLAgilyPack?

Can I use XmlDocument or my only choice are regular expressions ?

Misi
  • 748
  • 5
  • 21
  • 46

1 Answers1

0

u can use XML objects

like in http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectnodes.aspx

and use the XMLNode object to take what you want http://msdn.microsoft.com/en-us/library/System.Xml.XmlNode.aspx

you can use expressions to search what you want http://www.w3schools.com/xpath/xpath_syntax.asp

you can use this code to get a NODE

XmlDocument doc = new XmlDocument();
doc.LoadXml(myXMLContent); 
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode("MYTAG");
rodolfoprado
  • 126
  • 7
  • If I use XmlDocument doc = new XmlDocument(); doc.LoadXml(source); I get this error *Name cannot begin with the '0' character, hexadecimal value 0x30. Line 66, position 11.* On line 66 I have JS code : if(diff<0) – Misi Jul 31 '13 at 14:05
  • i'm using this code to get a NODE XmlDocument doc = new XmlDocument(); doc.LoadXml(myXMLContent); XmlNode root = doc.DocumentElement; XmlNode myNode = root.SelectSingleNode("MYTAG"); – rodolfoprado Jul 31 '13 at 19:02
  • 1
    @misi - yes, that's why the Html Agility Pack exists. If you can't use it, you'll have to do the parsing manually or using a regex. But that's not easy in the general case: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Simon Mourier Jul 31 '13 at 20:18
  • this HTML changes? you cant use a simple replace of to ?? – rodolfoprado Jul 31 '13 at 20:28
  • so you suggest using the C# string.Replace(). Or the Regex.Replace() – Misi Aug 01 '13 at 07:58