簡體   English   中英

C#-如何獲取子XML節點的數據

[英]c# - how can I get the data of sub XML node

我只是在學習C#而遇到問題。 我幾乎可以獲得所有數據。 但是,如何獲取此xml類別的數據?

 <data> <detail> <product id="183438053251143" type_ID="1" > <title>Test</title> <description>Testdescription</description> <category id="111" level_sub="TestSub" level_top="TestTopLevel"/> </product> <product id="183438053252420" type_ID="1"> <title>Title2</title> <description>Testdescription</description> <category id="123" level_sub="TestSub2" level_top="TestTopLevel2"/> </product> </detail> </data> 

該代碼有效-但我找不到用於獲取類別數據的解決方案。

 var products = from product in xml.Descendants("product") select product; foreach (var item in products) { productid = item.Attribute("id").Value; typeID = item.Attribute("type_ID").Value; string myproduct = string.Format("/data/detail/product[@id={0}]", productid ); XmlNodeList productList = xmlnode.SelectNodes(myproduct); foreach (XmlNode xnprogram in productList) { product_title = xnprogram["title"].InnerText.Trim(); product_title = product_title.Replace("'", ""); try { product_description = xnprogram["description"].InnerText.Trim(); } catch (Exception ex) { product_description = ""; } } } 

非常感謝。

var categoryElement=item.Element("category");
var idAttribute= categoryElement.Attribute("id");
var level_subAttribute=categoryElement.Attribute("level_sub");
....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM