Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Check valid XML element name.

Check valid XML element name.

Scheduled Pinned Locked Moved C#
xmltutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Libor Tinka
    wrote on last edited by
    #1

    How to check if the string can be a valid XML element name? In other words, I'm looking for this kind of function: bool XmlValidate(string name); XmlValidate("RightElement" ); // returns true XmlValidate("2 Wrong ,@# Element"); // returns false

    W L 2 Replies Last reply
    0
    • L Libor Tinka

      How to check if the string can be a valid XML element name? In other words, I'm looking for this kind of function: bool XmlValidate(string name); XmlValidate("RightElement" ); // returns true XmlValidate("2 Wrong ,@# Element"); // returns false

      W Offline
      W Offline
      whizzs
      wrote on last edited by
      #2

      I'm not sure about a validate function, but you could do it this way: using System.Text.RegularExpressions; . . . // reg ex to validate an XML element name Regex reg = new Regex ( @"^(?!xml)[a-zA-Z]\w+$", RegexOptions.IgnoreCase ); if ( reg.IsMatch ( "STRING TO CHECK" ) ) { // do something with it } I did not verify the regular expression string against all possible errors (malformed element names), but you should be able to correct any mistakes or add characters that you want to be valid. I don't like to use anything other than what is already there. Hope this helps

      1 Reply Last reply
      0
      • L Libor Tinka

        How to check if the string can be a valid XML element name? In other words, I'm looking for this kind of function: bool XmlValidate(string name); XmlValidate("RightElement" ); // returns true XmlValidate("2 Wrong ,@# Element"); // returns false

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        System.Xml.Serialization.CodeIdentifier.MakeValid() just check if the returned value equals the input value. xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups