我有一个需求,我需要为只有一个值的选择返回一个数组(在xml中),因为将使用数据的数据结构在其输入中期望一个整数数组(一个具有一个元素的数组)。
这就是我的XML文件的样子
<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:saw-sql="urn:saw-sql" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:complexType name="Row">
<xsd:sequence>
<xsd:element name="Column0" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""F&R Sales, Balances, Exposures and Limits"."Financial Calendar"."Month Year Short Name"" saw-sql:displayFormula=""Financial Calendar"."Month Year Short Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Financial Calendar" saw-sql:columnHeading="Month Year Short Name" saw-sql:isDoubleColumn="false" saw-sql:columnID="c29e370c0db340ad7" />
<xsd:element name="Column1" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=" CASE WHEN "F&R Sales, Balances, Exposures and Limits"."GRD Product Current"."GRD Product Level 5 Name" ='Bill financing' THEN 'Deposits' WHEN "F&R Sales, Balances, Exposures and Limits"."GRD Product Current"."GRD Product Level 5 Name" IN ('Operating lease','Finance lease') THEN 'Lending' WHEN "F&R Sales, Balances, Exposures and Limits"."GRD Product Current"."GRD Product Level 5 Name" ='Hire purchase and equipment loans' THEN 'Fee Based' ELSE "F&R Sales, Balances, Exposures and Limits"."GRD Product Current"."GRD Product Level 5 Name" END" saw-sql:displayFormula=" CASE WHEN "GRD Product Current"."GRD Product Level 5 Name" = 'Bill financing' THEN 'Deposits' WHEN "GRD Product Current"."GRD Product Level 5 Name" IN ('Operating lease', 'Finance lease') THEN 'Lending' WHEN "GRD Product Current"."GRD Product Level 5 Nam" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="GRD Product Current" saw-sql:columnHeading="Product" saw-sql:isDoubleColumn="false" saw-sql:columnID="c062984f028c318df" />
<xsd:element name="Column2" type="xsd:double" minOccurs="0" maxOccurs="1" saw-sql:type="double" saw-sql:sqlFormula=""F&R Sales, Balances, Exposures and Limits"."Balances - Spot"."Closing Balance"" saw-sql:displayFormula=""Balances - Spot"."Closing Balance"" saw-sql:aggregationRule="dimAggr" saw-sql:aggregationType="agg" saw-sql:tableHeading="Balances - Spot" saw-sql:columnHeading="Actual" saw-sql:isDoubleColumn="false" saw-sql:columnID="ca1e1d4f511a8bf93" />
<xsd:element name="Column3" type="xsd:double" minOccurs="0" maxOccurs="1" saw-sql:type="double" saw-sql:sqlFormula="("F&R Sales, Balances, Exposures and Limits"."Balances - Spot"."Closing Balance")*1.5" saw-sql:displayFormula=""Balances - Spot"."Closing Balance" * 1.5" saw-sql:aggregationRule="complex" saw-sql:aggregationType="agg" saw-sql:tableHeading="Balances - Spot" saw-sql:columnHeading="Forecast" saw-sql:isDoubleColumn="false" saw-sql:columnID="ca00747c911de130f" />
<xsd:element name="Column4" type="xsd:double" minOccurs="0" maxOccurs="1" saw-sql:type="double" saw-sql:sqlFormula=""F&R Sales, Balances, Exposures and Limits"."Limits"."Limit Amount"*2" saw-sql:displayFormula=""Limits"."Limit Amount" * 2" saw-sql:aggregationRule="complex" saw-sql:aggregationType="agg" saw-sql:tableHeading="Balances - Spot" saw-sql:columnHeading="Target" saw-sql:isDoubleColumn="false" saw-sql:columnID="c88758b2e449f89d0" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<Row>
<Column0>Sep-13</Column0>
<Column1>Deposits</Column1>
<Column2>330393232.5</Column2>
<Column3>495589848.75</Column3>
<Column4>703232974</Column4>
</Row>
<Row>
<Column0>Sep-13</Column0>
<Column1>Fee Based</Column1>
<Column2>111868709.42</Column2>
<Column3>167803064.13</Column3>
<Column4>703232974</Column4>
</Row>
<Row>
<Column0>Sep-13</Column0>
<Column1>Lending</Column1>
<Column2>18146873.33</Column2>
<Column3>27220309.995</Column3>
<Column4>703232974</Column4>
</Row>
</rowset>
我正在尝试做这样的事情来将值转换为整数数组。
var resultSet = (xmlDoc.Root.Descendants(ns + "Row").Select(result => new
{
MonthYearShortName = (DateTime) result.Element(ns + "Column0"),
Product = (String) result.Element(ns + "Column1"),
Actual = (decimal) result.Element(ns + "Column2"),
Forecast = (decimal) result.Element(ns + "Column3"),
Target = result.Elements(ns + "Column4").Select(s => Convert.ToDecimal(s.FirstNode)).ToArray()
}));
我得到一个异常,由于转换基础{System.SystemException} = {"Unable to cast object of type 'System.Xml.Linq.XText' to type 'System.IConvertible'."}
转换为decimal
而不是在Select
方法中使用Convert.ToDecimal
:
var resultSet = (xmlDoc.Root.Descendants(ns + "Row").Select(result => new
{
MonthYearShortName = (DateTime)result.Element(ns + "Column0"),
Product = (String)result.Element(ns + "Column1"),
Actual = (decimal)result.Element(ns + "Column2"),
Forecast = (decimal)result.Element(ns + "Column3"),
Target = result.Elements(ns + "Column4").Select(s => (decimal)s).ToArray()
}));
但是,这只有在您期望存在多个Column4
元素时才有意义。否则,您只需执行以下操作:
Target = (decimal)result.Element(ns + "Column4")
或获取decimal[]
只有一个值:
Target = new [] { (decimal)result.Element(ns + "Column4") }
根据您的编辑,您是否期望多个Column4条目?这个定义并没有让它看起来像这样,所以这个方法应该可以工作:
Target = (decimal)result.Element(ns + "Column4")
否则,如果您确实需要一个数组,则在FirstNode
调用中添加一个ToString
。
Target = result.Elements(ns + "Column4").Select(s => Convert.ToDecimal(s.FirstNode.ToString())).ToArray()
也就是说,不清楚为什么你用FirstNode
方法采取了不同的路线。这应该可以工作,对我来说似乎更清晰:
Target = result.Elements(ns + "Column4").Select(s => (decimal)s).ToArray()