下面是我用来挑选节点的XML结构。
<?xml version="1.0" encoding="utf-8"?>
<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:date" minOccurs="1" maxOccurs="1" saw-sql:type="date" saw-sql:sqlFormula=""F&R Sales, Balances, Exposures and Limits"."Financial Calendar"."Date"" saw-sql:displayFormula=""Financial Calendar"."Date"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Financial Calendar" saw-sql:columnHeading="Daily" saw-sql:isDoubleColumn="false" saw-sql:columnID="c1f8d3c16da4d2766" />
<xsd:element name="Column1" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="char" saw-sql:sqlFormula=""F&R Sales, Balances, Exposures and Limits"."Customer Details"."Customer Title"" saw-sql:displayFormula=""Customer Details"."Customer Title"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Details" saw-sql:columnHeading="Channel" saw-sql:isDoubleColumn="false" saw-sql:columnID="c7e5b7e356cc63ae3" />
<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="Closing Balance" saw-sql:isDoubleColumn="false" saw-sql:columnID="c5d3ad4e6d8e6cc66" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<Row>
<Column0>2013-08-30</Column0>
<Column1>Acquire</Column1>
<Column2>182629341.8</Column2>
</Row>
<Row>
<Column0>2013-08-30</Column0>
<Column1>Cross-sell</Column1>
<Column2>139272587.24</Column2>
</Row>
<Row>
<Column0>2013-08-30</Column0>
<Column1>Deepen</Column1>
<Column2>182862985.56</Column2>
</Row>
<Row>
<Column0>2013-08-31</Column0>
<Column1>Acquire</Column1>
<Column2>182629341.8</Column2>
</Row>
<Row>
<Column0>2013-08-31</Column0>
<Column1>Cross-sell</Column1>
<Column2>139272587.24</Column2>
</Row>
<Row>
<Column0>2013-08-31</Column0>
<Column1>Deepen</Column1>
<Column2>182862985.56</Column2>
</Row>
<Row>
<Column0>2013-09-01</Column0>
<Column1>Acquire</Column1>
<Column2>182742974.13</Column2>
</Row>
<Row>
<Column0>2013-09-01</Column0>
<Column1>Cross-sell</Column1>
<Column2>139423172.28</Column2>
</Row>
<Row>
<Column0>2013-09-01</Column0>
<Column1>Deepen</Column1>
<Column2>183136223.84</Column2>
</Row>
<Row>
<Column0>2013-09-02</Column0>
<Column1>Acquire</Column1>
<Column2>177144002.21</Column2>
</Row>
<Row>
<Column0>2013-09-02</Column0>
<Column1>Cross-sell</Column1>
<Column2>134426394.95</Column2>
</Row>
<Row>
<Column0>2013-09-02</Column0>
<Column1>Deepen</Column1>
<Column2>182969892.02</Column2>
</Row>
<Row>
<Column0>2013-09-03</Column0>
<Column1>Acquire</Column1>
<Column2>177120781.08</Column2>
</Row>
</rowSet>
基本上我要做的就是选择“Row”下的所有元素,在我的例子中是“Column0”,“Column1”和“Column2”。
以下是我用于获取结果的代码。不知何故,它不起作用。任何帮助都会有所帮助。
private static IEnumerable GenerateDataSet(XDocument xmlDoc)
{
XNamespace ns = @"urn:schemas-microsoft-com:xml-analysis:rowset";
IEnumerable resultSet = (from result in xmlDoc.Root.Descendants(ns + "Row")
select new
{
Date = DateTime.Parse(result.Element("Column0").Value),
KPI = (result.Attribute("Column1").Value),
Value = int.Parse(result.Attribute("Column2").Value)
});
return resultSet;
}
答案 0 :(得分:3)
ROW
和row
是两个不同的元素。Columns1
和Column2
是元素,而不是属性。Column0
/ Column1
/ Column2
的命名空间。DateTime
/ string
/ int
而不是Value
属性+ Parse
方法调用。它只是更好。<强>更新强>
Column2
包含decimal
/ double
值,而不是int
s:
XNamespace ns = @"urn:schemas-microsoft-com:xml-analysis:rowset";
IEnumerable resultSet = (from result in xmlDoc.Root.Descendants(ns + "ROW")
select new
{
Date = (DateTime)result.Element(ns + "Column0")),
KPI = (string)result.Element(ns + "Column1")),
Value = (decimal)result.Element(ns + "Column2"))
});
答案 1 :(得分:2)
ROW 和行不一样。 Xml是区分大小写的。您需要使用带小写的Row,因为这是您的元素名称。
答案 2 :(得分:0)
最后我正在使用这段代码:
returnValue = (from rows in doc.Root.Descendants(ns + "Row")
select (from columns in rows.Elements() select object)columns.Value).ToArray()
).ToList();