none
How to detect my xml has junk characters RRS feed

  • Question

  •             var str = "<AddDisCloserTab><Ticker>JCI</Ticker><CompanyName>Johnson Controls Qu颥c International plc&#xB;</CompanyName></AddDisCloserTab>";
    
    
    
    static bool IsValidXmlString(string text)
            {
                try
                {
                    XmlConvert.VerifyXmlChars(text);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    

    IsValidXmlString function returning true but there is junk is in my xml and those are Qu颥c & plc&#xB;

    when i try to execute the same xml in sql server then sql server throwing exception called illegal character in xml

    best give me best solution to detect junk chars in xml. thanks

    Thursday, July 25, 2019 9:27 AM

Answers

  • at my end XmlConvert.VerifyXmlChars() not raise error for this one plc&#xB; but this suppose to. thanks

    Since all of these characters are valid — ‘x’, ‘&’, ‘#’, ‘B’ — VerifyXmlChars do not have to raise any error.

    Examples of values that are not valid: ‘\x0B’, ‘\x00’, ‘\uFFFE’. They cannot be included into strings that are to be represented in XML.

     


    • Marked as answer by Sudip_inn Friday, July 26, 2019 8:02 PM
    Friday, July 26, 2019 8:14 AM

All replies

  • Seems to me that what you believe are invalid characters are not to IsValidXmlString

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 25, 2019 5:09 PM
    Moderator
  • In order to validate a file or string, you can use XmlDocument.Load, XmlDocument.LoadXml, XDocument.Load or XDocument.Parse.

    The function VerifyXmlChars probably checks if all of the characters of a string can be represented in XML. Unfortunately, 0x0B (Line Tabulation) is a special control character that cannot be included into XML, even if you write “&#xB;”.

    This different example correctly raises an exception: ‘XmlConvert.VerifyXmlChars( "International plc\x0B;" )’.


    • Edited by Viorel_MVP Thursday, July 25, 2019 6:01 PM
    Thursday, July 25, 2019 5:59 PM
  • Hi Sudip_inn, 

    Thank you for posting here.

    According to your problem, I have a question to confirm with you.

    How are your characters generated?

    XmlConvert.VerifyXmlChars() method is used to detect whether the characters in passed-in string are valid XML characters, so it cannot detect your junk characters.

    Considering the junk characters are irregular, the junk characters should avoid appearing at the beginning.

    Besides, I find a reference may help you.

    Detect Junk Characters in string

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 26, 2019 6:46 AM
    Moderator
  • at my end XmlConvert.VerifyXmlChars() not raise error for this one plc&#xB; but this suppose to. thanks
    Friday, July 26, 2019 8:04 AM
  • at my end XmlConvert.VerifyXmlChars() not raise error for this one plc&#xB; but this suppose to. thanks

    Since all of these characters are valid — ‘x’, ‘&’, ‘#’, ‘B’ — VerifyXmlChars do not have to raise any error.

    Examples of values that are not valid: ‘\x0B’, ‘\x00’, ‘\uFFFE’. They cannot be included into strings that are to be represented in XML.

     


    • Marked as answer by Sudip_inn Friday, July 26, 2019 8:02 PM
    Friday, July 26, 2019 8:14 AM