我刚学习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 = "";
}
}
}
非常感谢。
答案 0 :(得分:1)
var categoryElement=item.Element("category");
var idAttribute= categoryElement.Attribute("id");
var level_subAttribute=categoryElement.Attribute("level_sub");
....