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

.net entity-framework linq linq-to-xml

Question

My entity class with an entity looks like this:

[Table(Name = "CLINICAL_ITEM_MASTER")]
public class ClinicalItemMaster
{
    [Column]
    public int CLIENT_INPUT_MHS_ID { get; set; }
    [Column]
    public Guid CLIENT_INPUT_MHS_GUID { get; set; }
    [Column]
    public string ITEM { get; set; }
    [Column]
    public int ITEM_ID { get; set; }
    [Column]
    public string ITEM_NUMBER { get; set; }
    [Column]
    public string CATEGORY { get; set; }
    [Column]
    public string DESCRIPTION { get; set; }       
    [Column]
    public DateTime? CREATE_DTTM { get; set; }
    [Column]
    public DateTime? UPDATE_DTTM { get; set; }
}

And here I am using a Linq to XML(SQL) technique to get that database table's data:

private XElement GetClinicalItemMaster()
{
    try
    {
        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 I'm receiving the following error:

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

It is a column.cim.ITEM , according to my analysis, it is a Non-Nullable column, however when retrieving data from the database, it returns a null value (The data per this column is Null)

1
16
5/17/2016 9:20:39 AM

Accepted Answer

ZZZ_tmp
36
1/16/2014 6:49:50 AM

Popular Answer

Generally speaking, this occurs whenever you attempt to generate an XML file with spaces inside of any of its XElement/ nodes. Make sure your XML Node Names don't contain any spaces.

Example:

This will result in an error since the XElement or node "Customer Number" has a space, which is not permitted 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()
                                    select 
                                    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)))));
        xdoc.Save(@"D:\DynamicFile.xml"); 

Simply shift the space from "Customer Number" to "AccountNumber" in the XElement. That's it. So, if you've accidentally generated any spaces, look for them. I hope it can be of some assistance.



Related Questions





Related

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