转换xml时,带有空列的数据表会引发xml错误 [英] datatable with empty column is throwing xml error when converting xml
问题描述
大家好,
我知道这个问题已被多次询问,我通过实施其中一个博客提供的解决方案解决了这个问题,但我仍然面对这个问题。
这里我将一个数据表转换为xml,我可以期待移动没有列可以为空,需要在转换过程中正确处理异常。
以下是代码片段
Hi All,
I know this issue has been asked many times and I resolved this issue by implementing the solution provided from one of the blog but I am still facing the issue.
Here I am converting a datatable to xml where i can expect mobile no column can be empty and need to handle exception properly during conversion.
Below is the code snippet
ublic string convertGridDataToXML()
{
StringBuilder stb = new StringBuilder();
DataTable dt = (DataTable) Session["Datatable"];
// DataTable dt = (DataTable)GridView.DataSource;
try
{
if (dt!=null)
{
stb.Append("<employees>");
foreach (DataRow row in dt.Rows)
{
stb.Append("<names>");
stb.Append(string.Format("<staffid> {0} </staffid>", row[0].ToString()));
stb.Append(string.Format("<firstname> {0} </firstname>", row[1].ToString()));
stb.Append(string.Format("<lastname> {0} </lastname>", row[2].ToString()));
stb.Append(string.Format("<mobileno> {0} </mobileno>", (row[3].ToString() == " " ? " " : row[3].ToString())));
stb.Append(string.Format("<cardno> {0} </cardno>", row[4].ToString()));
stb.Append(string.Format("<email> {0} </email>", row[5].ToString()));
stb.Append("</names>");
}
stb.Append("</employees>");
}
else
{
stb.Append("<employees></employees>");
}
}
}'
当我将其提交到数据库时,我收到此错误引用未声明的实体'。第1行,位置155。有人可以帮助获得处理此异常的有效解决方案。
When I am submitting this to the database I am getting this error "Reference to undeclared entity 'nbsp'. Line 1, position 155" . Can some one provide help in getting the effective solution for handling this exception.
推荐答案
XML不明白。您可以在xml页面中将其添加到DOCTYPE元素中:
XML does not understand nbsp. You can define it in your xml page where you add it to your DOCTYPE element:
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
now you can use in your xml page!
or you can just use the number everywhere, which is less readable:  
参考:
1)如何将excel文件转换为xml格式
2) XmlSerializer空元素 - 空值
这篇关于转换xml时,带有空列的数据表会引发xml错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!