.net Linq to Xml:异常 - ' ' 字符,十六进制值 0x20,不能包含在名称中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21154989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:36:31  来源:igfitidea点击:

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

.netlinqentity-frameworklinq-to-xml

提问by MSanika

This is my Entity Class with an Entity :

这是我带有 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 :

在这里,我使用 Linq to XML(SQL) 方法访问数据库表数据:

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.

“[空格]”字符,十六进制值 0x20,不能包含在名称中。

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)

该列是cim.ITEM,根据我的分析,它是一个不可为空的列,但是从数据库获取数据时却为空(每列的数据为空)

回答by Sergey Berezovskiy

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

元素名称中有空格,这在 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.

删除空格以使元素名称有效。顺便说一句,将 null 作为元素值是完全可以的。

回答by Asif Iqbal

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.

如果您尝试在其任何 XElement/ 节点中创建带有空格的 XML 文件,通常会发生这种情况。确保 Xml 节点名称中没有任何空格。

Example:

例子:

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

这将引发错误,因为“帐号”XElement 或节点中有空格,这在 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.

只需将“ Account Number” XElement中的空格删除为“ AccountNumber”。大功告成。如果有任何错误创建的空间,请查找。希望它可以帮助某人。

回答by Mr.B

Possible reason for the same exception for someone who added the Descriptiondecorator with the white space as I did and faced the error. For instance:

对于Description像我一样添加带有空白的装饰器并面临错误的人来说,可能是因为同样的异常。例如:

[Description("My Property")]
public string MyProperty { get; set; } 

when you run:

当你运行时:

new XElement("MyProperty", myObject.MyProperty );

you'll have the same exception.

你会有同样的例外。