简体   繁体   English

C#-如何获取子XML节点的数据

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

I'am just learning c# and got a problem. 我只是在学习C#而遇到问题。 I can get nearly all data. 我几乎可以获得所有数据。 But how can I get the data of the category of this xml? 但是,如何获取此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> 

That code works - but I found no solution in order to get the category 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 = ""; } } } 

Thanks a lot. 非常感谢。

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