Null元素不会出现在XMLTextWriter结果中 [英] Null elements don't appear in XMLTextWriter results

查看:72
本文介绍了Null元素不会出现在XMLTextWriter结果中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行下面的代码,从数据表生成XML。但是对于每条记录,数据表中的一些

字段都是空的。假设field5有一个

null数据库值。我希望看到:


< field5>< / field5>或者< field5 />


但是它甚至根本没有显示那些

field5为空的记录!相反它只是显示:


< field4> Whatever< / field4>

< field6> Whatever< / field6>

这让我很担心,因为如果人们将XML文件输出并导入到

数据库中,他们甚至不知道field5存在,除非某些记录有

a非null值。但有时field5在所有记录中都为空。那么怎么做

我让作家写一个空的< field5 />代表所以数据库

进口不会被搞砸?在旁注中,我将在下面的代码中刷新

缓冲区?


这是我的代码:

公共共享函数GetXML(ByVal objDataTable As DataTable,ByVal

objXmlTextWriter As XmlTextWriter)As String

If IsNothing(objDataTable)Then


抛出新的异常(DataTable不可能是什么)

结束如果''结束如果没有(objDataTable)那么


Dim intCounter As Int32

objXmlTextWriter.WriteStartElement(objDataTable.Ta bleName)


对于intCounter = 0到objDataTable.Rows.Count - 1


objXmlTextWriter.WriteStartElement(" Row")

Dim objDataColumn As DataColumn

For each objDataColumn in objDataTable.Columns

objXmlTextWriter.WriteElementString(objDataColumn。ColumnName.ToString(),

objDataTable.Rows(intCounter).Item(objDataColumn.C olumnName).ToString())

下一页

objXmlTextWriter.WriteEndElement()

下一页


objXmlTextWriter.WriteEndElement()


返回objXmlTextWriter.ToString


结束功能

I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:

<field5></field5> or <field5 />

but instead it doesn''t even show the field at all for those records where
field5 is Null! Instead it just shows:

<field4>Whatever</field4>
<field6>Whatever</field6>

This concerns me because if people take XML file output and import into a
database, they won''t even know field5 exists unless some of the records have
a non null value. But sometimes field5 is null in all records. So how do
I get the writer to make an empty <field5 /> representation so database
imports won''t get screwed up? On a side note, where would I flush the
buffer in my code below?

Here''s my code:

Public Shared Function GetXML(ByVal objDataTable As DataTable, ByVal
objXmlTextWriter As XmlTextWriter) As String
If IsNothing(objDataTable) Then

Throw New Exception("DataTable cannot be nothing")
End If ''End of If Not Nothing(objDataTable) Then

Dim intCounter As Int32

objXmlTextWriter.WriteStartElement(objDataTable.Ta bleName)

For intCounter = 0 To objDataTable.Rows.Count - 1

objXmlTextWriter.WriteStartElement("Row")
Dim objDataColumn As DataColumn
For Each objDataColumn In objDataTable.Columns
objXmlTextWriter.WriteElementString(objDataColumn. ColumnName.ToString(),
objDataTable.Rows(intCounter).Item(objDataColumn.C olumnName).ToString())
Next
objXmlTextWriter.WriteEndElement()
Next

objXmlTextWriter.WriteEndElement()

Return objXmlTextWriter.ToString

End Function

推荐答案

" AFN" < DE ************************ @ yahoo.com>在留言中写道

news:19 *************** @ twister.socal.rr.com ...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@twister.socal.rr.com...
我是运行下面的代码从数据表生成XML。但是对于每条记录,数据表中的一些
字段都是空的。假设field5具有
null数据库值。我希望看到:

< field5>< / field5>或者< field5 />

但是它甚至根本没有显示那些
field5为空的记录!相反它只是显示:

< field4> Whatever< / field4>
< field6> Whatever< / field6>

这让我很担心,因为如果人们采取XML文件输出并导入到数据库中,除非某些记录
具有非空值,否则他们甚至不知道field5存在。但有时field5在所有记录中都为空。那么
我是如何让作家做空的< field5 />代表所以数据库进口不会被搞砸?


....

这是我的代码:
I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:

<field5></field5> or <field5 />

but instead it doesn''t even show the field at all for those records where
field5 is Null! Instead it just shows:

<field4>Whatever</field4>
<field6>Whatever</field6>

This concerns me because if people take XML file output and import into a
database, they won''t even know field5 exists unless some of the records have a non null value. But sometimes field5 is null in all records. So how do I get the writer to make an empty <field5 /> representation so database
imports won''t get screwed up?
....
Here''s my code:




这里''我的:


公共共享函数GetXML(ByVal dtDataTable As DataTable,ByVal

xwXmlTextWriter As XmlTextWriter)As String

if dtDataTable什么都没有呢

抛出新的异常(DataTable不可能是什么)''应该

真的使用ApplicationException

结束如果''结束如果objDataTable什么都没有那么


Dim intCounter as Int32

xwXmlTextWriter.WriteStartElement(dtDataTable.Tabl eName)

>
Dim row As DataRow

For the row in dtDataTable.Rows

xwXmlTextWriter.WriteStartElement(" Row")

Dim col As DataColumn

For each col in dtDataTable.Columns

xwXmlTextWriter.WriteElementString(col.ColumnName,

row(col).ToString() )

下一页

xwXmlTextWriter.WriteEndElement()

下一页

xwXmlTextWriter.WriteEndElement()

返回xwXmlTextWriter.ToString()

结束函数


一些评论:


1)如果您要使用类型缩写为变量添加前缀,我建议

反对使用obj。一切都是一个对象,所以这并没有告诉你

任何事情。

2)我通常使用For Each。循环而不是使用索引,除非我需要

来使用循环中的索引。索引只是你循环的事实

的工件。

3)你不必在字符串上使用ToString。 col.ColumnName已经是一个

字符串。


-

John Saunders

johnwsaundersiii at hotmail



Here''s mine:

Public Shared Function GetXML(ByVal dtDataTable As DataTable, ByVal
xwXmlTextWriter As XmlTextWriter) As String
If dtDataTable Is Nothing Then
Throw New Exception("DataTable cannot be nothing") '' Should
really use ApplicationException
End If ''End of If objDataTable Is Nothing Then

Dim intCounter As Int32

xwXmlTextWriter.WriteStartElement(dtDataTable.Tabl eName)

Dim row As DataRow
For Each row In dtDataTable.Rows
xwXmlTextWriter.WriteStartElement("Row")
Dim col As DataColumn
For Each col In dtDataTable.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName,
row(col).ToString())
Next
xwXmlTextWriter.WriteEndElement()
Next

xwXmlTextWriter.WriteEndElement()

Return xwXmlTextWriter.ToString()
End Function

Some comments:

1) If you''re going to prefix variables with a type abbreviation, I recommend
against using "obj". Everything is an object, so that doesn''t tell you
anything.
2) I generally use "For Each" loops instead of using an index unless I need
to use the index within the loop. The index is just an artifact of the fact
that you''re looping.
3) You don''t have to use ToString on strings. col.ColumnName is already a
string.

--
John Saunders
johnwsaundersiii at hotmail


" AFN" < DE ************************ @ yahoo.com>在留言中写道

news:19 *************** @ twister.socal.rr.com ...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@twister.socal.rr.com...
我是运行下面的代码从数据表生成XML。但是对于每条记录,数据表中的一些
字段都是空的。假设field5具有
null数据库值。我希望看到:

< field5>< / field5>或者< field5 />

但是它甚至根本没有显示那些
field5为空的记录!相反它只是显示:

< field4> Whatever< / field4>
< field6> Whatever< / field6>

这让我很担心,因为如果人们采取XML文件输出并导入到数据库中,除非某些记录
具有非空值,否则他们甚至不知道field5存在。但有时field5在所有记录中都为空。那么
我是如何让作家做空的< field5 />代表所以数据库进口不会被搞砸?


....

这是我的代码:
I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:

<field5></field5> or <field5 />

but instead it doesn''t even show the field at all for those records where
field5 is Null! Instead it just shows:

<field4>Whatever</field4>
<field6>Whatever</field6>

This concerns me because if people take XML file output and import into a
database, they won''t even know field5 exists unless some of the records have a non null value. But sometimes field5 is null in all records. So how do I get the writer to make an empty <field5 /> representation so database
imports won''t get screwed up?
....
Here''s my code:




这里''我的:


公共共享函数GetXML(ByVal dtDataTable As DataTable,ByVal

xwXmlTextWriter As XmlTextWriter)As String

if dtDataTable什么都没有呢

抛出新的异常(DataTable不可能是什么)''应该

真的使用ApplicationException

结束如果''结束如果objDataTable什么都没有那么


Dim intCounter as Int32

xwXmlTextWriter.WriteStartElement(dtDataTable.Tabl eName)

>
Dim row As DataRow

For the row in dtDataTable.Rows

xwXmlTextWriter.WriteStartElement(" Row")

Dim col As DataColumn

For each col in dtDataTable.Columns

xwXmlTextWriter.WriteElementString(col.ColumnName,

row(col).ToString() )

下一页

xwXmlTextWriter.WriteEndElement()

下一页

xwXmlTextWriter.WriteEndElement()

返回xwXmlTextWriter.ToString()

结束函数


一些评论:


1)如果您要使用类型缩写为变量添加前缀,我建议

反对使用obj。一切都是一个对象,所以这并没有告诉你

任何事情。

2)我通常使用For Each。循环而不是使用索引,除非我需要

来使用循环中的索引。索引只是你循环的事实

的工件。

3)你不必在字符串上使用ToString。 col.ColumnName已经是一个

字符串。


-

John Saunders

johnwsaundersiii at hotmail



Here''s mine:

Public Shared Function GetXML(ByVal dtDataTable As DataTable, ByVal
xwXmlTextWriter As XmlTextWriter) As String
If dtDataTable Is Nothing Then
Throw New Exception("DataTable cannot be nothing") '' Should
really use ApplicationException
End If ''End of If objDataTable Is Nothing Then

Dim intCounter As Int32

xwXmlTextWriter.WriteStartElement(dtDataTable.Tabl eName)

Dim row As DataRow
For Each row In dtDataTable.Rows
xwXmlTextWriter.WriteStartElement("Row")
Dim col As DataColumn
For Each col In dtDataTable.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName,
row(col).ToString())
Next
xwXmlTextWriter.WriteEndElement()
Next

xwXmlTextWriter.WriteEndElement()

Return xwXmlTextWriter.ToString()
End Function

Some comments:

1) If you''re going to prefix variables with a type abbreviation, I recommend
against using "obj". Everything is an object, so that doesn''t tell you
anything.
2) I generally use "For Each" loops instead of using an index unless I need
to use the index within the loop. The index is just an artifact of the fact
that you''re looping.
3) You don''t have to use ToString on strings. col.ColumnName is already a
string.

--
John Saunders
johnwsaundersiii at hotmail




" John Saunders" <乔************** @ notcoldmail.com>在消息中写道

news:um ************** @ TK2MSFTNGP11.phx.gbl ...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
" AFN" < DE ************************ @ yahoo.com>在消息中写道
新闻:19 *************** @ twister.socal.rr.com ...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@twister.socal.rr.com...
我正在运行下面的代码从数据表生成XML。但
数据表中的某些字段对于每条记录都是空的。假设field5的
为空数据库值。我希望看到:

< field5>< / field5>或者< field5 />

但是它甚至根本没有显示那些记录的字段
其中field5是空的!相反它只是显示:

< field4> Whatever< / field4>
< field6> Whatever< / field6>

这让我很担心,因为如果人们采取XML文件输出并导入
a数据库,他们甚至不知道field5存在,除非某些记录的
I am running the code below to generate XML from a data table. But some fields in the data table are Null for every record. Suppose field5 has a null database value. I would expect to see:

<field5></field5> or <field5 />

but instead it doesn''t even show the field at all for those records where field5 is Null! Instead it just shows:

<field4>Whatever</field4>
<field6>Whatever</field6>

This concerns me because if people take XML file output and import into a database, they won''t even know field5 exists unless some of the records have
非空值。但有时field5在所有记录中都为空。所以
a non null value. But sometimes field5 is null in all records. So



怎么做


how do

我让作家空白< field5 />代表所以数据库
进口不会被搞砸了?
...
I get the writer to make an empty <field5 /> representation so database
imports won''t get screwed up?
...
这是我的代码:



这是我的:

公共共享函数GetXML(ByVal dtDataTable As DataTable,ByVal
xwXmlTextWriter As XmlTextWriter)As String
如果dtDataTable什么都没有那么真的使用ApplicationException
结束如果''结束如果objDataTable什么都没有那么

Dim intCounter As Int32

xwXmlTextWriter.WriteStartElement(dtDataTable.Tabl eName)

Dim row As DataRow
For dtDataTable.Rows中的每一行
xwXmlTextWriter.WriteStartElement(" ;行")
Dim col As DataColumn
For each col in dtDataTable.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName ,
row(col).ToString())
下一页
xwXmlTextWriter.WriteEndElement()
xwXmlTextWriter.WriteEndElement()

返回xwXmlTextWriter.ToString()
结束函数

一些评论:

1)如果要使用类型缩写为变量添加前缀,我



Here''s mine:

Public Shared Function GetXML(ByVal dtDataTable As DataTable, ByVal
xwXmlTextWriter As XmlTextWriter) As String
If dtDataTable Is Nothing Then
Throw New Exception("DataTable cannot be nothing") '' Should
really use ApplicationException
End If ''End of If objDataTable Is Nothing Then

Dim intCounter As Int32

xwXmlTextWriter.WriteStartElement(dtDataTable.Tabl eName)

Dim row As DataRow
For Each row In dtDataTable.Rows
xwXmlTextWriter.WriteStartElement("Row")
Dim col As DataColumn
For Each col In dtDataTable.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName,
row(col).ToString())
Next
xwXmlTextWriter.WriteEndElement()
Next

xwXmlTextWriter.WriteEndElement()

Return xwXmlTextWriter.ToString()
End Function

Some comments:

1) If you''re going to prefix variables with a type abbreviation, I



建议不要使用obj。一切都是一个对象,所以不告诉你什么。
2)我通常使用For Each。循环而不是使用索引,除非I
需要在循环中使用索引。索引只是你循环的
事实的一个神器。
3)你不必在字符串上使用ToString。 col.ColumnName已经是一个
字符串。

- John Saunders
johnwsaundersiii at hotmail


recommend against using "obj". Everything is an object, so that doesn''t tell you
anything.
2) I generally use "For Each" loops instead of using an index unless I need to use the index within the loop. The index is just an artifact of the fact that you''re looping.
3) You don''t have to use ToString on strings. col.ColumnName is already a
string.

--
John Saunders
johnwsaundersiii at hotmail



我感谢您的代码建议。谢谢。我没有写函数

而且你是绝对正确的。


但是,它仍然没有解决我丢失的#1问题< field5 />当

field5为空时。你知道我该如何解决这个问题吗?



I appreciate your code suggestions. Thanks. I did not write the function
and you are absolutely correct.

BUT, it still doesn''t solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


这篇关于Null元素不会出现在XMLTextWriter结果中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆