c# - 表到 xml 记录 c#

标签 c# xml linq

我需要在我的 SQL Server 数据库中的同一列中存储许多记录,为此我使用 XML 字段。在后端,我有读取/写入 xml 记录并将数据插入表中的代码。该表是可变大小。我在从表中读取字段并将它们插入 xml 变量时遇到问题。 我需要得到的是:

<BigTag>
<SmallTagName1>  something</SmallTagName1>
<SmallTagName2>  thanksForanswering</SmallTagName2>
<SmallTagName3>  youareawesome</SmallTagName3>
</BigTag>

表格是:

smalltagTable[] = {something,thanksForanswering,youareawesome,....}

这是我的代码:

XElement WidgetListE1 = null;

WidgetListE1 = new XElement("BigTag"  );

string smalltagname = "smalltag ";
string smalltagdata;

for(int i=0;i<smalltagTable.Length; i++ )
{
    SmallTagname = "smallTagName " + i;
    smalltagdata = smalltagTable[i];
    WidgetListE1.Add(new XElement(smallTagName.ToString(),SmalltagData.ToString() ));
}

string a2 = WidgetListE1.ToString();

问题是这段代码遇到了错误:

“”字符,十六进制值 0x20,不能包含在名称中。 在线:

WidgetListE1.Add(new XElement(smallTagName.ToString(),SmalltagData.ToString() ));

最佳答案

我发布这个只是为了确保答案清楚地出现。致谢 Panagiotis Kanavos。问题是 xml 的标记中有一个空格。而不是:

 <smalltag 1> something </smalltag 1> 

应该是

<smalltag1> something</smalltag1>

关于c# - 表到 xml 记录 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577506/

相关文章:

xml - 在 Delphi 中使用 XML(将特定数据返回给变量)

java - xml解析+Java ME

c# - 在 Linq 中发出对象 where 子句 : the condition is bypassed

c# - 使用 LINQ 从具有 n 个元素的属性填充集合

c# - 执行具有任何签名的方法的方法

c# - 使用抽象接口(interface)成员的具体实现来实现接口(interface)

c# - 如何以编程方式检测副作用(编译时或运行时)?

c# - 如何在 Xunit 中创建基测试类来测试接口(interface)实现?

java - Spring RestTemplate 无法解码包含 "&lt;!DOCTYPE ... dtd>"的 XML

c# - 使用 LINQ 返回子对象