Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable XML validation when using XDocument

I'm parsing an XLIFF document using the XDocument class. Does XDocument perform some validation of the content which I read into it, and if so - is there any way to disable that validation?

I'm getting some weird errors if the XLIFF isn't valid XML (I don't care that it isn't, I just want to parse it).

E.g.

'.', hexadecimal value 0x00, is an invalid character. 

I'm currently reading the file like this:

string FileLocation = @"C:\XLIFF\text.xlf";
XDocument doc = XDocument.Load(FileLocation);

Thanks.

like image 975
Jimmy Collins Avatar asked Dec 13 '22 14:12

Jimmy Collins


2 Answers

I had similar problem which was fixed by letting StreamReader to read the content.

// this line throws exception like yours
XDocument xd = XDocument.Load(@"C:\test.xml");

// works
XDocument xd = XDocument.Load(new System.IO.StreamReader(@"C:\test.xml"));

If that does not help, try to include proper encoding.

like image 61
HABJAN Avatar answered Jan 03 '23 18:01

HABJAN


If you want to strip characters from strings that are invalid for use in XML, you can use this method:

private static string RemoveXmlInvalidCharacters(string s)
{
    return Regex.Replace(
        s,
        @"[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]",
        string.Empty);
}

It removes any characters that fall outside of the set of valid character values, according to the XML standard.

like image 21
Drew Noakes Avatar answered Jan 03 '23 18:01

Drew Noakes