{"action":"opened","issue":{"url":"https://api.github.com/repos/scrapy/scrapy/issues/1428","labels_url":"https://api.github.com/repos/scrapy/scrapy/issues/1428/labels{/name}","comments_url":"https://api.github.com/repos/scrapy/scrapy/issues/1428/comments","events_url":"https://api.github.com/repos/scrapy/scrapy/issues/1428/events","html_url":"https://github.com/scrapy/scrapy/issues/1428","id":100445126,"number":1428,"title":"XML serialiser corrupts output when encountering None values","user":{"login":"Malvineous","id":171656,"avatar_url":"https://avatars.githubusercontent.com/u/171656?v=3","gravatar_id":"","url":"https://api.github.com/users/Malvineous","html_url":"https://github.com/Malvineous","followers_url":"https://api.github.com/users/Malvineous/followers","following_url":"https://api.github.com/users/Malvineous/following{/other_user}","gists_url":"https://api.github.com/users/Malvineous/gists{/gist_id}","starred_url":"https://api.github.com/users/Malvineous/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Malvineous/subscriptions","organizations_url":"https://api.github.com/users/Malvineous/orgs","repos_url":"https://api.github.com/users/Malvineous/repos","events_url":"https://api.github.com/users/Malvineous/events{/privacy}","received_events_url":"https://api.github.com/users/Malvineous/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-12T01:07:01Z","updated_at":"2015-08-12T01:07:01Z","closed_at":null,"body":"If you set a field to the value `None` then use the XML serialiser, the output XML becomes corrupted (invalid XML, missing tags, etc.) and you get the following exception for each item during the scrapy run:\r\n\r\n```\r\n2015-08-12 11:01:43 [scrapy] ERROR: Error caught on signal handler: <bound method ?.item_scraped of <scrapy.extensions.feedexport.FeedExporter object at 0x7f70e7723ed0>>\r\nTraceback (most recent call last):\r\n  File \"/usr/lib/python2.7/site-packages/twisted/internet/defer.py\", line 150, in maybeDeferred\r\n    result = f(*args, **kw)\r\n  File \"/usr/lib/python2.7/site-packages/scrapy/xlib/pydispatch/robustapply.py\", line 57, in robustApply\r\n    return receiver(*arguments, **named)\r\n  File \"/usr/lib/python2.7/site-packages/scrapy/extensions/feedexport.py\", line 193, in item_scraped\r\n    slot.exporter.export_item(item)\r\n  File \"/usr/lib/python2.7/site-packages/scrapy/exporters.py\", line 130, in export_item\r\n    self._export_xml_field(name, value)\r\n  File \"/usr/lib/python2.7/site-packages/scrapy/exporters.py\", line 146, in _export_xml_field\r\n    self._xg_characters(serialized_value)\r\n  File \"/usr/lib/python2.7/site-packages/scrapy/exporters.py\", line 157, in _xg_characters\r\n    serialized_value = serialized_value.decode(self.encoding)\r\nAttributeError: 'NoneType' object has no attribute 'decode'\r\n```\r\n\r\nThis was happening for me because I was using a CSS selector and `extract_first()`, but the element was optional, so for those items where the element was missing, the field was being set to `None`, causing this problem."}}