使用LINQ获取XML

时间:2014-03-16 17:16:57

标签: c# xml linq linq-to-xml

下面是我用来挑选节点的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="&quot;F&amp;R Sales, Balances, Exposures and Limits&quot;.&quot;Financial Calendar&quot;.&quot;Date&quot;" saw-sql:displayFormula="&quot;Financial Calendar&quot;.&quot;Date&quot;" 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="&quot;F&amp;R Sales, Balances, Exposures and Limits&quot;.&quot;Customer Details&quot;.&quot;Customer Title&quot;" saw-sql:displayFormula="&quot;Customer Details&quot;.&quot;Customer Title&quot;" 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="&quot;F&amp;R Sales, Balances, Exposures and Limits&quot;.&quot;Balances - Spot&quot;.&quot;Closing Balance&quot;" saw-sql:displayFormula="&quot;Balances - Spot&quot;.&quot;Closing Balance&quot;" 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;
}

3 个答案:

答案 0 :(得分:3)

  1. XML区分大小写,因此您必须完全匹配元素名称才能使其正常工作。 ROWrow是两个不同的元素。
  2. Columns1Column2是元素,而不是属性。
  3. 您还必须使用Column0 / Column1 / Column2的命名空间。
  4. 使用显式转换为DateTime / string / int而不是Value属性+ Parse方法调用。它只是更好。
  5. <强>更新

    1. 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();