我正在尝试解析xml文件。它给了root的所有节点但是我只想要子节点,不想要孙子节点。我怎么能得到那些? 我的xml是:
constraints
并且为了得到我写过语句的节点:
<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>
所以在这个xml 我不想 产品和交易的子节点 请帮帮我..
答案 0 :(得分:0)
答案 1 :(得分: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
}
}