Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name

.net entity-framework linq linq-to-xml


This is my Entity Class with an Entity :

public class ClinicalItemMaster
    public int CLIENT_INPUT_MHS_ID { get; set; }
    public Guid CLIENT_INPUT_MHS_GUID { get; set; }
    public string ITEM { get; set; }
    public int ITEM_ID { get; set; }
    public string ITEM_NUMBER { get; set; }
    public string CATEGORY { get; set; }
    public string DESCRIPTION { get; set; }       
    public DateTime? CREATE_DTTM { get; set; }
    public DateTime? UPDATE_DTTM { get; set; }

And Here I am accessing that Database Table data using Linq to XML(SQL) approach :

private XElement GetClinicalItemMaster()
        using (MyDatabase db = new MyDatabase())
            return new XElement("CLINICALITEMMASTER",
                   from cim in db.TblClinicalItemMaster                           
                   select new XElement("Record",
                       new XElement("CLIENT_INPUT_MHS_ID", cim.CLIENT_INPUT_MHS_ID),
                       new XElement("CLIENT_INPUT_MHS_GUID", cim.CLIENT_INPUT_MHS_GUID.ToString()),
                       new XElement("ITEM ", cim.ITEM),
                       new XElement("ITEM_ID ", cim.ITEM_ID),
                       new XElement("ITEM_NUMBER ", cim.ITEM_NUMBER.ToString()),
                       new XElement("CATEGORY ", cim.CATEGORY.ToString()),
                       new XElement("DESCRIPTION ", cim.DESCRIPTION),
                       new XElement("MFG_CODE ", cim.MFG_CODE)      ));

But here I am getting this error:

The '[white space]' character, hexadecimal value 0x20, cannot be included in a name.

The column is cim.ITEM, as per my analysis its a Non-Nullable column but While getting data from DataBase getting Null(The data per this column is Null)

5/17/2016 9:20:39 AM

Accepted Answer

You have white spaces in elements names, which is not allowed in XML:

new XElement("ITEM ", cim.ITEM), // starting from this element
//                ^ here

Remove white spaces in order to make element names valid. BTW it's completely OK to have null as element value.

1/16/2014 6:49:50 AM

Popular Answer

This generally happens if you are trying to create XML file with spaces in any of its XElement/ nodes. Make sure you don't have any spaces in Xml Node Names.


This will throw error as there is a space in "Account Number" XElement or node,which is not allowed in XML.

XDocument xdoc = new XDocument(
                            new XDeclaration("1.0", "utf-8", "yes"),
                            new XComment("Create Sample Xml from Dyanamic Data"),
                            new XElement("Company",
                                new XElement("Employees",
                                    from e in DAL.GetEmp()
                                    new XElement("Employee", new XAttribute("id", e.Id),
                                        new XElement("EmpName", e.Name),
                                        new XElement("Designation", e.Designation),
                                        new XElement("Location", e.Location),
                                        new XElement("Account Number", e.Account),
                                        new XElement("PAN", e.Pan),
                                        new XElement("PF", e.PF)))));

Just remove the space in "Account Number" XElement to "AccountNumber". It's done.So look for the spaces if any you have mistakenly created. Hope it might help someone.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow