2016-12-29 165 views
0

我想解析一個xml文件。它給予root的所有節點,但我只需要子節點,不需要孫子節點。我怎樣才能得到這些? 我的XML是:如何只獲取xml中的根節點的節點在c#

<Pdf_Format> 
    <SpaceSep>0</SpaceSep> 
    <text>   GLOBUS</text> 
    <LineSep>2</LineSep> 
    <text>***************************************</text> 
    <LineSep>1</LineSep> 
    <SpaceSep>3</SpaceSep> 
    <text>Maxus Mall 2nd Floor S/11Timba</text> 
    <LineSep>1</LineSep> 
    <SpaceSep>3</SpaceSep> 
    <text>Near Hyover Bridge Bhayander</text> 
    <LineSep>1</LineSep> 
    <SpaceSep>0</SpaceSep> 
    <text>   022-28170870</text> 
    <LineSep>2</LineSep> 
    <text>=======================================</text> 
    <LineSep>1</LineSep> 
    <text>=======================================</text> 
    <LineSep>1</LineSep> 
    <param>ReprintBill</param> 
    <LineSep>1</LineSep> 
    <SpaceSep>3</SpaceSep> 
    <text>Qty</text> 
    <SpaceSep>4</SpaceSep> 
    <text>Item</text> 
    <SpaceSep>3</SpaceSep> 
    <SpaceSep>6</SpaceSep> 
    <text>Price</text> 
    <SpaceSep>5</SpaceSep> 
    <text>Total</text> 
    <SpaceSep>2</SpaceSep> 
    <LineSep>1</LineSep> 
    <SpaceSep>3</SpaceSep> 
    <text>---</text> 
    <SpaceSep>4</SpaceSep> 
    <text>----</text> 
    <SpaceSep>3</SpaceSep> 
    <SpaceSep>6</SpaceSep> 
    <text>-----</text> 
    <SpaceSep>5</SpaceSep> 
    <text>-----</text> 
    <SpaceSep>2</SpaceSep> 
    <LineSep>1</LineSep> 
    <product> 
     <SpaceSep>3</SpaceSep> 
     <param>Qty</param> 
     <SpaceSep>3</SpaceSep> 
     <param>ItemID</param> 
     <SpaceSep>1</SpaceSep> 
     <param>PriceSubDisc</param> 
     <SpaceSep>1</SpaceSep> 
     <param>TotalSubDisc</param> 
     <LineSep>1</LineSep> 
     <param>TN</param> 
     <SpaceSep>6</SpaceSep> 
     <param>Desc</param> 
     <LineSep>1</LineSep> 
     <SpaceSep>16</SpaceSep> 
     <param>ActualPrice</param> 
     <LineSep>1</LineSep> 
     <param>Reason</param> 
     <LineSep>1</LineSep> 
    </product> 
    <LineSep>2</LineSep> 
    <SpaceSep>11</SpaceSep> 
    <text>Sub Total </text> 
    <SpaceSep>5</SpaceSep> 
    <param>SubTotal</param> 
    <LineSep>1</LineSep> 
    <SpaceSep>11</SpaceSep> 
    <text>Tax   </text> 
    <SpaceSep>5</SpaceSep> 
    <param>Tax</param> 
    <LineSep>2</LineSep> 
    <SpaceSep>11</SpaceSep> 
    <text>Final Total </text> 
    <SpaceSep>5</SpaceSep> 
    <param>FinalTotal</param> 
    <Transactions> 
     <LineSep>1</LineSep> 
     <SpaceSep>11</SpaceSep> 
     <param>Mode</param> 
     <SpaceSep>5</SpaceSep> 
     <param>Amount</param> 
    </Transactions> 
    <LineSep>1</LineSep> 
    <SpaceSep>1</SpaceSep> 
    <text>Sales Associate:</text> 
    <param>CashierName</param> 
    <LineSep>1</LineSep> 
    <text>========================================</text> 
    <LineSep>1</LineSep> 
    <text>CST TIN:27020000519C W.E.F. 01-04-2006</text> 
    <LineSep>1</LineSep> 
    <text>VAT TIN:27020000519V W.E.F. 01-04-2006</text> 
    <LineSep>1</LineSep> 
    <text>No Exchange and No Refunds</text> 
    <LineSep>1</LineSep> 
    <text>========================================</text> 
    <LineSep>1</LineSep> 
    <SpaceSep>1</SpaceSep> 
    <text>Trx</text> 
    <SpaceSep>2</SpaceSep> 
    <param>BillId</param> 
    <SpaceSep>2</SpaceSep> 
    <param>StoreCode</param> 
    <SpaceSep>2</SpaceSep> 
    <param>Counter</param> 
    <SpaceSep>2</SpaceSep> 
    <param>BillDate</param> 
    <SpaceSep>2</SpaceSep> 
    <param>BillTime</param> 
    <LineSep>6</LineSep> 
</Pdf_Format> 

並獲得我已經寫了聲明的節點:

XDocument loadedData = XDocument.Load(XMLFilePath); 
foreach (XElement node in loadedData.Descendants()) 

所以在這個XML 我不想產品交易 的的childNodes請幫助我..

+0

XPath和XPathSelectElements() – MMK

+0

您是否想要完全忽略Product和Transactions元素?或者只是這些孩子的節點? – MMK

+0

後裔()給孩子和孫子,而元素()只給孩子。 – jdweng

回答

0

您可以使用Elements

以文檔順序返回此元素或文檔的子元素的集合。

例子:

foreach (XElement node in loadedData.Elements()) 
0

我解決我自己這樣。

foreach (XElement node in loadedData.Descendants()) 
      { 
       if ((node.Parent != null && (node.Parent.Name.LocalName == "product"))) 
       { 
        if ((node.Parent != null && node.Parent.Name.LocalName == "Transactions")) 
        { } 
       } 

       else 
       { 
       //some code here 
       } 
      }