Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is my Entity Class with an Entity :

[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 accessing that Database Table data using Linq to XML(SQL) approach :

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 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)

like image 794
MSanika Avatar asked Jan 16 '14 06:01

MSanika


2 Answers

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.

like image 69
Sergey Berezovskiy Avatar answered Oct 30 '22 00:10

Sergey Berezovskiy


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.

Example:

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()
                                    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"); 

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.

like image 4
Asif Iqbal Avatar answered Oct 30 '22 00:10

Asif Iqbal