2011-12-01 23 views
0

我已经要分析这个XML具有XML解析 - 没有得到正确的方式

<category id="1"> 
    <title>Environment & Heritage</title> 
    <subcategories> 
     <subcategory id="2">Trees & Green Cover</subcategory> 
     <subcategory id="3">Noise Pollution</subcategory> 
     <subcategory id="4">Air Pollution</subcategory> 
     <subcategory id="5">Water Pollution</subcategory> 
</category> 
<category id="72"> 
    <title>Environment and Heritage</title> 
    <subcategories/> 
</category> 
<category id="7"> 
    <title>Health & Sanitation</title> 
    <subcategories><subcategory id="8">Drains & Sewerage</subcategory> 
    <subcategory id="9">Solid Waste Management</subcategory> 
    </subcategories> 
</category> 

类是

public class Categories 
{ 
    public string Id { get; set; } 
    public string Title { get; set; } 
    public List<Categories> subCategories { get; set; } 

    public Categories() { } 

    public Categories(string value, string text) 
    { 
     this.Id = value; 
     this.Title = text; 
    } 
} 

我们有这个类List

的对象名单此功能正在做主要工作

List<Categories> FillObjectFromXML(string xmString) 
    { 
     //Declaration 
     XDocument xmlDoc = XDocument.Load(new StringReader(xmString)); 
     List<Categories> categoriesList = new List<Categories>(); 
     Categories catItem;// = new Categories(); 
     Categories subItem; 
     List<Categories> subCategoriesList;// = new List<Categories>(); 

     //Coding 
     var lv1s = from lv1 in xmlDoc.Descendants("category") 
        select new 
        { 
         Id = lv1.Attribute("id").Value, 
         Header = lv1.Descendants("title"), 
         Children = lv1.Descendants("subcategories") 
        }; 

     //Loop through results 
     foreach (var lv1 in lv1s) 
     { 
      catItem = new Categories(); 
      catItem.Id = lv1.Id; 
      catItem.Title = lv1.Header.First().Value; 
      subCategoriesList = new List<Categories>(); 
      foreach (var lv2 in lv1.Children) 
      { 
       subItem=new Categories(); 
       subItem.Id=lv2.Attribute("id").Value; 
       subItem.Title=lv2.Descendants("title").ToString(); 
       subCategoriesList.Add(subItem); 
      } 
      catItem.subCategories = subCategoriesList; 
      categoriesList.Add(catItem); 
     } 


     //End 
     return categoriesList; 
    } 

lv2的foreach循环没有得到正确的结果

+1

您的第一个XML节点类别ID 1没有子类别结束标记。 – Josh

+1

加上没有根元素,但我把这是一个复制/粘贴错误,因为代码甚至不会将XML加载到XDocument中。 – dash

+0

m很抱歉,复制/粘贴错误 – 1Mayur

回答

2

在你上面的方法,我相信您需要更改行:

Children = lv1.Descendants("subcategories") 

到:

Children = lv1.Descendants("subcategory") 

如果你喜欢XPath,你可以简化这段代码。

等级类别之一:

XmlNodeList categories = xmlDoc.SelectNodes("//category"); //assumes there is only one tier of categories. 

然后,您可以全面的foreach每个类别:

foreach(XmlNode category in categories) 
{ 
    XmlNodeList subcategories = category.SelectNodes("./subcategories/subcategory"); 
} 

从而消除大部分的DOM的走,你必须做的。取决于你是否喜欢XPath--但它对此很好。

+0

选择的coz解释与XmlNode的替代:) – 1Mayur

1

您的XML当前无效。固定,你正在分析你的Children集合时找错了现场后 - 每个孩子subcategories节点 - 你希望它是一个subcategory节点,但 - 所以改变这样的:

Children = lv1.Descendants("subcategories") 

Children = lv1.Descendants("subcategory")