c# - Different behaviors between XmlDocument.LoadXml and XDocument.Parse -


our project has been converted use xdocument xmldocument few days ago, found strange behavior while processing xml entity in attribute value xdocument.parse, sample code following:

  1. the xml string:

    string xml = @"<char symbol=""&#x0;"">";

  2. the xmldocument.loadxml code , result:

        xmldocument xmldocument = new xmldocument();     xmldocument.loadxml(xml);     console.writeline(xmldocument.outerxml); 

    result:

    <char symbol="&#x0;" />

  3. the xdocument.parse code , exception:

        xdocument xdocument = xdocument.parse(xml);     console.writeline(xdocument.tostring()); 

    exception:

    a first chance exception of type 'system.xml.xmlexception' occurred in system.xml.dll '.', hexadecimal value 0x00, invalid character. line 1, position 18. @ system.xml.xmltextreaderimpl.throw(exception e) @ system.xml.xmltextreaderimpl.throw(string res, string[] args) @ system.xml.xmltextreaderimpl.throw(int32 pos, string res, string[] args) @ system.xml.xmltextreaderimpl.parsenumericcharrefinline(int32 startpos, boolean expand, stringbuilder internalsubsetbuilder, int32& charcount, entitytype& entitytype) @ system.xml.xmltextreaderimpl.parsenumericcharref(boolean expand, stringbuilder internalsubsetbuilder, entitytype& entitytype) @ system.xml.xmltextreaderimpl.handleentityreference(boolean isinattributevalue, entityexpandtype expandtype, int32& charrefendpos) @ system.xml.xmltextreaderimpl.parseattributevalueslow(int32 curpos, char quotechar, nodedata attr) @ system.xml.xmltextreaderimpl.parseattributes() @ system.xml.xmltextreaderimpl.parseelement() @ system.xml.xmltextreaderimpl.parsedocumentcontent() @ system.xml.xmltextreaderimpl.read() @ system.xml.linq.xdocument.load(xmlreader reader, loadoptions options) @ system.xml.linq.xdocument.parse(string text, loadoptions options) @ system.xml.linq.xdocument.parse(string text)

it seems "&#x0;" invalid character, change value valid character such "&#x60;" both methods worked well.

is there way change xdocument.parse behavior ignore invalid character in attribute xmldocument.loadxml does?

according this arctice value � invalid. i've experienced myself xdocument class follows xml standard stricter xmldocument (which think thing).

read article, give suggestions how around error.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -