如何使用整个XML结构获取ReadXml来构建DataSet,而不仅仅是第一个节点?
我正在使用DataSet.ReadXml(MemoryStream)读取SSRS XML steam的结果。
将SSRS导出为XML错误“名为'Value'的列已属于DataTable:无法设置同名的嵌套表名。
我得到错误,因为一些子节点有空白属性,而其他子节点不基本上,第一个Parent有子节点具有所有空白子属性,而第二个Parent有一些子节点具有其属性值。当ReadXml构建数据集时,它使用第一个Parent的结构,然后在第二个父项中读取时发生炸弹。
我正在调用具有不同日期范围的相同SSRS报告。使用较短的日期范围,当数据较少时我会收到错误。如果日期范围较长,则问题不存在。除了节点如:
之外,XML的结构不会改变<TrendingChartGroup2 Label="Sep

2013">
<Value Y="0"/>
成为这个:
<TrendingChartGroup2 Label="Jun

2013">
<Value/>
DataSet.ReadXml(MemoryStream)是否基于它看到的第一个XML节点构建DataSet结构? 有没有办法在加载数据集之前读入XML结构,这样就不会自动创建从它找到的前几个节点创建的DataSet?
以下是显示已知良好且已知的错误xml节点和值的XML片段。
已知好处:
<?xml version="1.0" encoding="utf-8"?>
<Report xsi:schemaLocation="MyReport2 http://reportserver?%2FMyReport2&rs
%3AFormat=XML&rc%3ASchema=True" Name="MyReport2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="MyReport2">
<Rehosp30DaysCurrDxChart1>
<Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_DxGroupName Label="Episode Of Care
As Treatment DxGroupName">
<TrendingChartGroup2_Collection>
<TrendingChartGroup2 Label="Oct

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Nov

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Dec

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jan

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Feb

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Mar

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Apr

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="May

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jun

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jul

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Aug

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Sep

2013">
<Value Y="0"/>
</TrendingChartGroup2>
</TrendingChartGroup2_Collection>
</Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_DxGroupName>
<Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_Pneumonia Label="Episode Of
Care As Treatment Pneumonia">
<TrendingChartGroup2_Collection>
<TrendingChartGroup2 Label="Oct

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Nov

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Dec

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jan

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Feb

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Mar

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Apr

2013">
<Value Y="1"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="May

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jun

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jul

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Aug

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Sep

2013">
<Value Y="0"/>
</TrendingChartGroup2>
</TrendingChartGroup2_Collection>
</Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_Pneumonia>
</Rehosp30DaysCurrDxChart1>
</Report>
已知坏:
<?xml version="1.0" encoding="utf-8"?>
<Report xsi:schemaLocation="MyReport2 http://reportserver?%2FMyReport2&rs
%3AFormat=XML&rc%3ASchema=True" Name="MyReport2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="MyReport2">
<Rehosp30DaysCurrDxChart1>
<Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_DxGroupName Label="Episode Of Care
As Treatment DxGroupName">
<TrendingChartGroup2_Collection>
<TrendingChartGroup2 Label="Jul

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Aug

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Sep

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Oct

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Nov

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Dec

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jan

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Feb

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Mar

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Apr

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="May

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jun

2013">
<Value/>
</TrendingChartGroup2>
</TrendingChartGroup2_Collection>
</Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_DxGroupName>
<Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_Pneumonia Label="Episode Of
Care As Treatment Pneumonia">
<TrendingChartGroup2_Collection>
<TrendingChartGroup2 Label="Jul

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Aug

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Sep

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Oct

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Nov

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Dec

2012">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jan

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Feb

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Mar

2013">
<Value Y="0"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Apr

2013">
<Value Y="1"/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="May

2013">
<Value/>
</TrendingChartGroup2>
<TrendingChartGroup2 Label="Jun

2013">
<Value Y="0"/>
</TrendingChartGroup2>
</TrendingChartGroup2_Collection>
</Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_Pneumonia>
</Rehosp30DaysCurrDxChart1>
</Report>
任何帮助将不胜感激。我不明白DataSet.ReadXml(MemoryStream)如何构建数据集。
谢谢!
编辑:我应该在数据集中添加我需要它,因为现有代码在整个地方使用数据集。有没有办法将XDocument转到DataSet来解决这个问题?
答案 0 :(得分:0)
我根本不会在这种情况下使用DataSet.Readxml。重新思考读取xml的方法可能是个好主意。 .NET提供了许多方法来执行此操作。以下是一些要审查的文章。
然后您的问题变为如何对SSRS输出进行单元测试。此外,作为单元测试人员,您想要测试xml的SSRS结构或结构中提供的值吗?
测试结构
我将SSRS xml加载到Visual Studio中,并使用XML菜单生成xml架构(XSD)文档。很明显,架构设计包含来自报表MDX查询的元数据,因此如果要验证生成的xml结构是否正确,您可能需要创建一些模板文档并替换一些元素名称然后使用一些.net xml结构验证方法。见http://support.microsoft.com/kb/307379
<xs:element name="Rehosp30DaysCurrDxChart1">
<xs:complexType>
<xs:sequence>
<xs:element name="Episode_x0020_Of_x0020_Care_x0020_From_x0020_Hospital_x0020_DxGroupName">
<xs:complexType>
<xs:sequence>
<xs:element name="TrendingChartGroup2_Collection">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="TrendingChartGroup2">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" />
</xs:sequence>
<xs:attribute name="Label" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Label" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
测试数据
单元测试中的文本xml数据可以通过多种方式完成。您可以遍历xml dom或使用xPath确认值。这可能会使您的单元测试有点复杂。
发送原始xml输出文本
最后,如果您有一组好的xml结果,您可以将SSRS中的原始xml文本与“预期的xml结果”进行比较,作为上述两种方法的简单替代方法。虽然我没有使用它,但您可能需要检查任何xml单元测试工具,如http://xmlunit.sourceforge.net/